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?
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)
You can set the font size argument, figsize change how Matplotlib treats fonts in general, or even change the figure size.
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.
use cbar.ax.tick_params(labelsize=10)
From here and here
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With