Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

matplotlib remove the ticks (axis) from the colorbar

I want to remove the (ticks) axis with numbers to the right of the colorbar. I am using matplotlib with python as follows:

f = plt.figure()
ax = f.add_subplot(1,1,1)
i = ax.imshow(mat, cmap= 'gray')
cbar = f.colorbar(i)

enter image description here

like image 290
Daisy Avatar asked May 11 '19 22:05

Daisy


People also ask

How do I get rid of tick marks in MatPlotLib?

To remove the ticks on the y-axis, tick_params() method has an attribute named left and we can set its value to False and pass it as a parameter inside the tick_params() function. It removes the tick on the y-axis.

How do I hide axis text ticks or tick labels in MatPlotLib?

By default, in the Matplotlib library, plots are plotted on a white background. Therefore, setting the color of tick labels as white can make the axis tick labels hidden. For this only color, the attribute needs to pass with w (represents white) as a value to xticks() and yticks() function.

How do you turn Xticks off?

By using the method xticks() and yticks() you can disable the ticks and tick labels from both the x-axis and y-axis. In the above example, we use plt. xticks([]) method to invisible both the ticks and labels on the x-axis and set the ticks empty. plt.


1 Answers

If you just want to remove the ticks but keep the ticklabels, you can set the size of the ticks to be 0 as following

f = plt.figure()
ax = f.add_subplot(1,1,1)
mat = np.arange(100).reshape((10, 10))
i = ax.imshow(mat, cmap= 'viridis')
cbar = f.colorbar(i)
cbar.ax.tick_params(size=0)

enter image description here

If you want to remove both, the ticks and the labels, you can use set_ticks([]) by passing an empty list.

cbar.set_ticks([])

enter image description here

like image 169
Sheldore Avatar answered Oct 19 '22 03:10

Sheldore