I am new to matplotlib and struggling to plot a colorBar with custom values.
More specificly, I need to plot only a color bar, not the related figure, and for this I have a list of color values , as well as the corresponding values for each of these colors;
I tried essentially two different methods but cant get what I want :
import matplotlib as mpl
from matplotlib import pyplot as plt
colors = [ #1A6AD0, #3D98E3, #79C4E9, #055A05, #99C48C, #AC8C1A ]
values = [ -1800 , -1200, -1, 0, 600 , 1200 ]
fig = plt.figure( figsize=(2,4) )
ax = fig.add_axes([0, 0.05, 0.25, 0.9])
# First option :
cmap = LinearSegmentedColormap.from_list("mypalette", colors, N=1000)
norm = mpl.colors.Normalize(vmin=min(values), vmax=max(values))
cb = mpl.colorbar.ColorbarBase(ax, cmap=cmap, norm=norm, orientation='vertical') # also tried to add ticks=values here but no ..doesnt work .. Color palette has a nice gradient but values do not match their colors. not even with spacing = 'uniform' or 'proportional' ...
# Second option :
norm2 = mpl.colors.BoundaryNorm(values, cmap.N)
cb = mpl.colorbar.ColorbarBase(ax, cmap=cmap, norm=norm2, spacing='proportional' , orientation='vertical', ticks=values ) # Values correspond to the rights colors, but there is no gradient
So, my problem is that in the first case, I manage to have a nice color gradient, but the values ( which will be the ticks) do no appear at the right "color position" ( for example green faces 600 while it should face 100) All the trouble comes from the fact that my values are not evenly spaced I think. But I cannot change them !
In the second case, i have the proper correspondence between colors and values, but the colors are now discrete (appear by blocs), which is really annoying ( and not only for visual purpose)
I am especially expecting a quick color transition from blue to green for the values corresponding to -1 and 0
Unfortunately I cannot show here the pictures as I using a secure computer to program, but what I obtained in the first and second method are similar to the first two pictures in this page : https://matplotlib.org/tutorials/colors/colorbar_only.html#sphx-glr-tutorials-colors-colorbar-only-py
Of course, I what I would like to obtain, is a ColorBar with both a nice color gradient AND the right correspondence between colors <-> values
I have found and read different examples on the web especially here and on matplotlib documentation, but damn ... impossible
Am I missing something obvious ?
Thanks a lot
[english is not my native language]
I suppose you want to provide the corresponding positions of the colors when creating the colormap. Else, colors are equally distributed within the range of 0 to 1.
When supplying the positions, you cannot use the values directly, but need to map them to the unit interval. For this the same norm
can be used as for creating the colorbar.
import matplotlib as mpl
from matplotlib import pyplot as plt
from matplotlib.colors import LinearSegmentedColormap
colors = [ "#1A6AD0", "#3D98E3", "#79C4E9", "#055A05", "#99C48C", "#AC8C1A"]
values = [ -1800 , -1200, -1, 0, 600 , 1200 ]
fig = plt.figure( figsize=(2,4) )
ax = fig.add_axes([0, 0.05, 0.25, 0.9])
norm = mpl.colors.Normalize(vmin=min(values), vmax=max(values))
normed_vals = norm(values)
cmap = LinearSegmentedColormap.from_list("mypalette", list(zip(normed_vals, colors)), N=1000)
cb = mpl.colorbar.ColorbarBase(ax, cmap=cmap, norm=norm, orientation='vertical')
plt.show()