Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change fontsize of colorbars in matplotlib

I am having difficulty adjusting the font size of the ticks on the colorbar in the following code.

fig = plt.figure(figsize=(10,6))

ax = fig.add_subplot(111)
im = ax.pcolor(np.ma.masked_invalid(np.roll(lon, -1100, axis=1)[:2100, :3500]), 
           np.ma.masked_invalid(np.roll(lat, -1100, axis=1)[:2100, :3500]), 
           np.ma.masked_invalid(np.roll(np.absolute(zeta_Mar), -1100, axis=1)[:2100, :3500]),
              cmap='Reds', norm=colors.LogNorm(vmin=1e-6, vmax=1e-4))
ax.set_xlabel('Longitude', fontsize=14)
ax.set_xlabel('Latitude', fontsize=14)
cbar_axim = fig.add_axes([0.95, 0.15, 0.03, 0.7])
cbar = fig.colorbar(im, cax=cbar_axim, ticks=[1e-6, 1e-5, 1e-4])
cbar.set_ticklabels([r'$-10^{-6}$', r'$10^{-5}$', r'$10^{-4}$'])
cbar.set_label(r'$\zeta\ [s^{-1}]$', fontsize=16)

plt.show()

Could anyone tell me the correct syntax to include the fontsize argument?

like image 286
takachanbo Avatar asked Oct 21 '16 20:10

takachanbo


People also ask

How do I change the color bar size in Matplotlib?

Fraction parameter in colorbar() is used to set the size of colorbar in Python. Using this we can match colorbar size to graph as: If vertical colorbar is used, then fraction=0.047 * (height_of_image / width_of_image) If horizontal colorbar is used, then fraction=0.047 * (width_of_image / height_of_image)

Can you change font size in Matplotlib?

You can set the font size argument, figsize change how Matplotlib treats fonts in general, or even change the figure size.

How do you change the font size of a title in a Matplotlib figure?

pyplot. title() method to assign a title to a plot, so in order to change the font size, we are going to use the font size argument of the pyplot. title() method in the matplotlib module.


2 Answers

use cbar.ax.tick_params(labelsize=10)

From here and here

like image 86
Kennet Celeste Avatar answered Oct 22 '22 21:10

Kennet Celeste


If I use @Yugi's answer, I will get latex errors. You can also set the fontsize with:

ticklabs = cbar.ax.get_yticklabels()
cbar.ax.set_yticklabels(ticklabs, fontsize=10)
like image 7
TheIdealis Avatar answered Oct 22 '22 20:10

TheIdealis