I have some values (between 3 and 6) that I want to plot in a medium-light grey with a thick line plot (about lw=20). However, when the percentage change between two points is greater than some threshold and positive, I want the colour to progress along a gradient to green. When the percentage change between two points is less than some threshold and negative, I want the colour to progress along a gradient to red. Otherwise, I want the colour to remain the original grey.
I start by defining some random mock data:
import numpy as np
import matplotlib.pyplot as plt
x = np.arange(0, np.random.randint(3, 6), 1)
y = np.random.normal(0, 1, len(x))
And then my idea was to interpolate 100 new datapoints evenly spaced between every two datapoints, then define the colour for each of those datapoints based on condition.
R, G, B = 0.5, 0.5, 0.5
red = (1, 0, 0)
all_y = np.array([])
for i, yval in enumerate(y):
if i == 0:
continue
diff = y[i]-y[i-1]
step = diff/100
if diff != 0.0:
y_grain = np.arange(y[i-1], y[i], step)
else:
y_grain = np.repeat(y[i], 100)
all_y = np.append(all_y, y_grain)
# Set all colours
r,g,b = R, G, B # temp rgb vals to update according to pct change
if diff < -1:
pass # colour progressive red
elif diff > 1:
pass # colour progressive green
else:
# colour grey
# clrs = [(r,g,b,1) for j in y_grain]
# Set the colours
all_x = np.arange(0, len(all_y)/100, 0.01)
plt.plot(all_x, all_y, lw=15, c=clrs)
One problem is that the clrs array (of RGBA values) does not work for some reason (I thought I did it this way before). Is there a better solution to this?