How can I draw a x-y line with matplotlib that has a fill-between color which depends on a third variable z? (where z is of same length as x and y).
I would like something like this:
What I am getting:
Sample code looks like this:
import numpy as np
import matplotlib.pyplot as plt
cm = plt.get_cmap('gist_rainbow')
x = np.linspace(0, 10, 100)
y = np.sin(x) + 1.3
z = 0.5*(np.cos(x) + 1.0)
fig = plt.figure(111)
ax = plt.axes(xlim=(0.0, 10.0), ylim=(0.0, 3.0))
line, = ax.plot([], [], lw=2)
line.set_data(x, y)
ax.fill_between(x, y, interpolate=True, color= cm(z))
plt.show()
I have tried with and without interpolate=True but makes no difference here.
Have scanned similar questions/answers but haven't found anything that would help me solve this. Any suggestions would be much appreciated.


