takachanbo
takachanbo

Reputation: 683

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?

Upvotes: 19

Views: 89304

Answers (3)

Alejo Bernardin
Alejo Bernardin

Reputation: 1215

If you are trying to increase the font size but some numbers disappear because of big size, you can do

cbar = plt.colorbar()
for t in cbar.ax.get_yticklabels():
     t.set_fontsize(20)

Upvotes: 7

TheIdealis
TheIdealis

Reputation: 707

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)

Upvotes: 9

Kennet Celeste
Kennet Celeste

Reputation: 4771

use cbar.ax.tick_params(labelsize=10)

From here and here

Upvotes: 49

Related Questions