[英]Seaborn, change font size of the colorbar
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)
.我能够使用
plt.tick_params(axis='both', labelsize=20)
更改刻度标签。 However, the colorbar font size does not change.但是,颜色条字体大小不会改变。 Is there a way to do that?
有没有办法做到这一点?
You can use matplotlib.axes.Axes.tick_params with labelsize.您可以将matplotlib.axes.Axes.tick_params与 labelsize 一起使用。
For example, your plot with labelsize 20:例如,您的标签大小为 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 - 使用 matplotlib.colorbar.Colorbar 对象
- 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 .您可以通过改变字体放大
seaborn.set()
方法中的设置font_scale
参数去你想要的规模,多看seaborn的文档。
For example, your plot with scale 3:例如,您的比例为 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 set the following, it will increase all text in the graph by a factor of two.如果您设置以下内容,它将使图表中的所有文本增加两倍。 However, if you immediately set the tick_params to lower right after, you will be left with just the font size of the colorbar increased.
但是,如果您立即将 tick_params 设置为右下角,您将只剩下颜色条的字体大小增加。
sns.set(font_scale=2)
sns.heatmap(df, vmin=0, vmax=1, center=0.5)
heatmap.tick_params(labelsize=15)
sns.set(font_scale=1)
Don't forget to set the font_scale back :)不要忘记重新设置 font_scale :)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.