1

I am trying to create a plot with a Colorbar. I am using the shared code from Shan Dou in this post: Matplotlib - add colorbar to a sequence of line plots

The code is working when I am using the values defined in Shan Dou code and it gives me this plot:

enter image description here

But when I change the values of the Colorbar, it's not working and I am not getting the desired colors as in the previous plot for all the points. See the graphic :

enter image description here

Is there a way to sort this problem? Here is my attempt to adapt Shan Dou's code:

import numpy as np
import matplotlib.pyplot as plt
import matplotlib as mpl

vol =  np.array([1.99423194, 2.05616212, 2.15023971, 2.25328779, 2.33402443])
x = np.linspace(0, 10, 5)
y = x[:, None]+ np.linspace(0, 4, 5)

norm = mpl.colors.Normalize(vmin=vol.min(), vmax=vol.max())
cmap = mpl.cm.ScalarMappable(norm=norm, cmap=mpl.cm.jet)
cmap.set_array([])

fig, ax = plt.subplots(dpi=100)
for i, yi in enumerate(y.T):
    ax.plot(x, yi,'o', c=cmap.to_rgba(i + 1))
fig.colorbar(cmap, ticks=vol)

0 Answers0