Matplotlib Color Gradient Based on Y-Value
I Would Like to Plot a "Cut" Through a Heat Map, I. E. Apply a Color Gradient to My Plt. Plot(X, Y) Based on the Y-Values (Which Are in a Range of -0.5,0.5...
I would like to plot a "cut" through a heat map, i.e. apply a color gradient to my plt.plot(x,y) based on the y-values (which are in a range of -0.5,0.5.
Any suggestions? I tried to workaround by using a scatter-plot, but the colormap seemed to be applied to each line individually (and not globally).
Thanks!
EDIT
To describe it in other terms: I would like to map each y-value in the interval to an appropriate color in my colormap. (The phrase 'cut through heat map' just refers to the commonly used technique to map the z-values of a function z=f(x,y) for given x and y to a color.)
As mentioned earlier already, just applying a colormap to a scatter-plot seems to to map the maximum y-value of each line (as i am plotting multiple series) to white, as seen in the attachment. Instead, I would like to map the global y=0.5 in my plot to white and -0.5 to black for each line.
1 Answer
Turns out i needed to normalize my colormap by using
norm = colors.Normalize(vmin=-min, vmax=max)
and then call plt.scatter(x, y, c=y, norm=norm, …).