3

I am trying to plot a line that should be colored in a way that represents the trend of the graph. For example, if it is increasing it should be green, while if it is decreasing it should be red.

I can represent this trend plotting dots simply using a shifted dataframe:

dates = ['2018-01-{}'.format(d) for d in range(1, 32)]
vals = [1, 2, 3, 4, 6, 9, 12, 11, 10, 8, 4, 10, 15, 17, 17, 18, 18, 17, 16, 19, 22, 23, 23, 25, 28, 33, 30, 25, 24,
        20, 18]

df = pd.DataFrame(data=vals, columns=['Value'])
df.set_index(pd.to_datetime(dates), inplace=True)

df_shifted = df.shift()
df_shifted.iloc[0] = df_shifted.iloc[1]
mask_inc = df >= df_shifted
df['Increase'] = mask_inc['Value']

fig, ax = plt.subplots()
ax.plot(df['Value'], color='#ededed')

color = {True: 'green', False: 'red'}
for index, row in df.iterrows():
    ax.plot(index, row['Value'], 'o', color=color[row['Increase']])

enter image description here

I know that matplotlib does not allow using different colors in the same line plot, but is there any workaround to this without making it utterly complicated?

I have thought about plotting two different dataframes using the Increase mask but the problem is that the line would be plotted continuedly, so all dots would be connected, while I would need it to be split into different pieces made up of segments.

dabadaba
  • 9,064
  • 21
  • 85
  • 155

2 Answers2

1

Look at DataFrame.diff. Don't think it gets any simpler than that.

Found this answer, I think using that you should get the segments you need, like this:

dates = ['2018-01-{}'.format(d) for d in range(1, 32)]
vals = [1, 2, 3, 4, 6, 9, 12, 11, 10, 8, 4, 10, 15, 17, 17, 18, 18, 17, 16, 19, 22, 23, 23, 25, 28, 33, 30, 25, 24,
        20, 18]

df = pd.DataFrame(data=vals, columns=['Value'])
df.set_index(pd.to_datetime(dates), inplace=True)
df['difference'] = df.diff()
df['condition'] = (df.difference > 0).astype(int)
df['group'] = df.condition.diff().abs().cumsum().fillna(0).astype(int) + 1

fig, ax = plt.subplots()
# fail safe only
ax.plot(df.Value, color='blue')

# decides if starts in descend
# (first difference is NaN therefore first condition 0 no matter what)
red = df.condition.iloc[1] == 1
last = pd.DataFrame()
for i in range(df.group.max() + 1):
    group = pd.concat([last, df.Value[df.group == i]])
    last = group.iloc[-1:]
    red = not red

    ax.plot(group, color='red' if red else 'green')

It should result what you've been looking for, without any gaps.

axx
  • 41
  • 4
1

You can follow up this tutorial to achieve what you want.

You may then use below code for such:

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.collections import LineCollection
from matplotlib.colors import ListedColormap, BoundaryNorm
import datetime

max_range = 32
dates = ['2018-01-{}'.format(d) for d in range(1, max_range)]
x = np.asarray(range(1,max_range))
y = [1, 2, 3, 4, 6, 9, 12, 11, 10, 8, 4, 10, 15, 17, 17, 18, 18, 17, 16, 19, 22, 23, 23, 25, 28, 33, 30, 25, 24,
        20, 18]
y = np.asarray(y)
z = [i - j for i, j in zip(y[:-1], y[1:])]
z = np.asarray(z)

# Create a colormap for red, green and blue and a norm to color
# f' < -0.5 red, f' > 0.5 blue, and the rest green
cmap = ListedColormap(['g', 'b', 'r'])
norm = BoundaryNorm([-100, -0.5, 0.5, 100], cmap.N)

# Create a set of line segments so that we can color them individually
# This creates the points as a N x 1 x 2 array so that we can stack points
# together easily to get the segments. The segments array for line collection
# needs to be numlines x points per line x 2 (x and y)
points = np.array([x, y]).T.reshape(-1, 1, 2)
segments = np.concatenate([points[:-1], points[1:]], axis=1)

# Create the line collection object, setting the colormapping parameters.
# Have to set the actual values used for colormapping separately.
lc = LineCollection(segments, cmap=cmap, norm=norm)
lc.set_array(z)
lc.set_linewidth(3)

fig1 = plt.figure()
plt.gca().add_collection(lc)
plt.xlim(0,max_range-1)
plt.ylim(min(y), max(y))
plt.xticks(x,dates, rotation='vertical')
plt.tight_layout()
plt.show()

Which produces below plot graph: slope colored plot

Cedric Zoppolo
  • 4,271
  • 6
  • 29
  • 59