I wanted to change the font size for a heatmap colorbar.
The following is my code:
import seaborn as sns
import matplotlib.pyplot as plt
from numpy import arange
x = arange(25).reshape(5, 5)
cmap = sns.diverging_palette(220, 20, sep=20, as_cmap=True)
ax = sns.heatmap(x, cmap=cmap)
plt.show()
I was able to change the tick labels with plt.tick_params(axis='both', labelsize=20)
. However, the colorbar font size does not change.
Is there a way to do that?
MatPlotLib with Python Create a colorbar with a scalar mappable object image. Initialize a variable for fontsize to change the tick size of the colorbar. Use axis tick_params() method to set the tick size of the colorbar. To display the figure, use show() method.
We can change the configurations and theme of a seaborn plot using the seaborn. set() function. To set the font size, we use the font_scale parameter in this function. This parameter automatically alters the font of everything in the graph, from the legend to both the axis labels and everything.
You can use matplotlib.axes.Axes.tick_params with labelsize.
For example, your plot with labelsize 20:
import seaborn as sns
import matplotlib.pyplot as plt
from numpy import arange
x = arange(25).reshape(5, 5)
cmap = sns.diverging_palette(220, 20, sep=20, as_cmap=True)
ax = sns.heatmap(x, cmap=cmap)
# use matplotlib.colorbar.Colorbar object
cbar = ax.collections[0].colorbar
# here set the labelsize by 20
cbar.ax.tick_params(labelsize=20)
plt.show()
I refered to the following answer:
- Using matplotlib.colorbar.Colorbar object
- Setting parameter
You can change the font scale with the seaborn.set()
method setting the font_scale
param to the scale you want, see more in seaborn documentation.
For example, your plot with scale 3:
import seaborn as sns
import matplotlib.pyplot as plt
from numpy import arange
# here set the scale by 3
sns.set(font_scale=3)
x = arange(25).reshape(5, 5)
cmap = sns.diverging_palette(220, 20, sep=20, as_cmap=True)
ax = sns.heatmap(x, cmap=cmap)
plt.show()
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