How do I set no "ticks" at all in the color bar?

60 views Asked by At

I want to set the colorbar with no ticks in any form. I've searched and set the tick parameter at two places. However, still there are some ticks at right boarder.

cb = mpl.colorbar.ColorbarBase(ax2, cmap=cmap, norm=norm, spacing='proportional', boundaries=bounds, format='%.1f')

ax2.set_xlabel('t', size=12)
cb.ax.tick_params(size=0)  # these two lines are my searching effort
cb.set_ticks([])
plt.show()

enter image description here

Edit

Here is a self contained code, still has tiny ticks.

fig, ax = plt.subplots(1, 1, figsize=(0.05, 6))
tmax=5
tmin=-5
color_num=6
bounds = np.linspace(tmin, tmax, color_num+1)
norm = mpl.colors.BoundaryNorm(bounds, color_num)
cmap = plt.cm.tab20
cmaplist = [cmap(i) for i in range(color_num)]
cmap = mpl.colors.LinearSegmentedColormap.from_list('custom map', cmaplist, color_num)
cb = mpl.colorbar.ColorbarBase(ax, cmap=cmap, norm=norm, spacing='proportional')

cb.ax.tick_params(size=0)  # these two lines are my searching effort
cb.set_ticks([])
plt.show()

enter image description here

2

There are 2 answers

2
Luca On BEST ANSWER

It looks like the tiny ticks are generated by cb.set_ticks([]) when no label is specified (I am using matplotlib 3.7.3).

Though it is more a workaround rather than a solution, one could hide the ticks setting the Axes.tick_params. Indeed, one can minimize the ticks and label sizes, and set them to the same color of the figure background, so that they are not visible anymore.
To do so:

cb.ax.tick_params(size=0,                   # tick size
                  labelsize=0,              # label size
                  colors=ax.get_facecolor() # tick and labels color
                  )

which results in

colorbar with hidden ticks

1
cphlewis On

Create the colorbar with an empty ticklist:

cb = mpl.colorbar.ColorbarBase(ax, cmap=cmap, norm=norm, 
                               ticks = [],  spacing='proportional')

segmented colorbar, no ticks