Issue
I made this colorbar:
import matplotlib.pyplot as plt
import matplotlib as mpl
fig, ax = plt.subplots(figsize=(6, 1))
fig.subplots_adjust(bottom=0.5)
cmap = mpl.cm.viridis
norm = mpl.colors.Normalize(vmin=0, vmax=1)
cb1 = mpl.colorbar.ColorbarBase(ax, cmap=cmap,
norm=norm,
orientation='horizontal')
cb1.set_label('Some Units')
fig.show()
but I am trying to apply this colorbar to this colormap of random variables using plt.contourf (I need to use plt.contourf for my actual data set, so I cannot use plt.imshow. I know plt.imshow does the correct colormap but in this case, I cannot use plt.imshow).
data = np.random.rand(100,200,144)
x = plt.contourf(data[-1],cmap=cmap,norm=norm)
plt.colorbar(x)
plt.show()
Why is the colorbar not going from 0 - 1 but has the intervals from 0 - 1.05? How do I replicate the created colorbar (cb1) to apply it to the data?
Solution
plt.contourf()
seems to do some rounding to have "nice" values for its levels. You could explicitly set your levels, for example levels=np.linspace(0, 1, 11)
to obtain 10
regions (11
region edges).
import numpy as np
import matplotlib as mpl
cmap = mpl.cm.viridis
norm = mpl.colors.Normalize(vmin=0, vmax=1)
data = np.random.rand(100, 200, 144)
cntr = plt.contourf(data[-1], cmap=cmap, norm=norm, levels=np.linspace(0, 1, 11))
plt.colorbar(cntr)
plt.show()
Answered By - JohanC
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.