I am looking for solutions to:
- Align vertically the tick labels along the colorbar with the center of each color/class.
- Place some tick labels beside both colorbar triangular extensions.
- Change the figure size to see the tick labels completely while keeping the same colorbar width.
I already tried various solutions proposed on stack overflow to center the tick labels, but none seems to work properly with my current code. For exemple, using ax.set_yticks(number_of_classes + 0.5) or other similiar methods put all the tick labels at the bottom of the colorbar instead of placing them aligned with the center of each color.
This is the colorbar I get with my current code.
Here's the code I used:
import matplotlib as mpl
from matplotlib.backends.backend_agg import FigureCanvasAgg
from matplotlib.colors import LinearSegmentedColormap
from matplotlib.figure import Figure
import numpy as np
# data and labels
bounds = [44, 51, 53, 56]
color_palette = [[43, 131, 186],
[86, 104, 146],
[129, 78, 107],
[172, 51, 67],
[215, 25, 28]]
tick_labels = ['Convection not likely',
'Likely thunderstorms',
'Isolated severe storms',
'Widely scattered severe storms',
'Scattered severe storms']
# Array of colors with rgb value in 0-1 range
all_vals = np.array([[color_palette[0][0] / 255,
color_palette[0][1] / 255,
color_palette[0][2] / 255,
1]])
for i in range(1, len(color_palette)):
vals = [[color_palette[i][0] / 255,
color_palette[i][1] / 255,
color_palette[i][2] / 255,
1]]
all_vals = np.concatenate((all_vals, vals))
# create a colormap
cmap = mpl.colors.ListedColormap(all_vals[1:-1])
cmap.set_over(all_vals[-1])
cmap.set_under(all_vals[0])
# normalize bound values
norm = mpl.colors.BoundaryNorm(bounds, cmap.N)
# create a figure
fig = Figure(figsize=(2, 6))
canvas = FigureCanvasAgg(fig)
ax = fig.add_subplot(121)
# create the colorbar
cb = mpl.colorbar.ColorbarBase(ax, cmap=cmap,
norm=norm,
extend='both',
ticks=bounds,
spacing='uniform')
# add the tick labels
cb.ax.set_yticklabels(tick_labels)
fig.savefig('colorbar')
