I've been searching a little (link1, link2) this subject and it seems not to be that trivial, and I didn't find any definite solution to it. The idea is to fill the area between two lines with a continuous gradient of color, and if possible, use any of the available colormaps of matplotlib.
My solution for the moment is:
import numpy as np
import matplotlib.pyplot as plt
np.random.seed(123)
x = np.linspace(0, 15, 125)
y1 = np.random.random(125)
y2 = np.random.random(125)-2
red = (1,0,0)
blue = (0,0,1)
fig, ax = plt.subplots(1,1, figsize=(12,6))
ax.plot(x, y1, c='red')
ax.plot(x, y2, c='blue')
npts = 100
r = np.linspace(red[0],blue[0],npts)
g = np.linspace(red[1],blue[1],npts)
b = np.linspace(red[2],blue[2],npts)
colors = np.vstack((r,g,b)).T
for i in range(npts):
ax.fill_between(x, y1-((y1-y2)/npts)*i, y1-((y1-y2)/npts)*(i+1), facecolor=colors[i], alpha=0.7)
fig.show()
The ideal solution I am looking for is to get a graph as in this:
Source of the image
As you can see, here one end of the colormap is set on the maximum of all values, and the other on the minimum of all values. In my solution, the maximum and minimum are set for each value of X. Here it seems to be using the 'viridis' colormap, but would be nice to be able to choose any colormap or to directly choose the colors at each end.
I appreciate any help.
Thanks
EDIT: I add the image of my first solution. The solution I wanted is given here. Thanks Mr. T, for pointing the solution.