Seaborn,更改颜色B的字体大小

2024-04-29 00:57:20 发布

您现在位置:Python中文网/ 问答频道 /正文

我想更改热图颜色栏的字体大小。

以下是我的代码:

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()

我可以用plt.tick_params(axis='both', labelsize=20)更改tick标签。但是,颜色栏字体大小不变。 有办法吗?

enter image description here


Tags: 代码fromimportmatplotlib颜色aspltseaborn
2条回答

可以使用seaborn.set()方法更改字体比例,将font_scale参数设置为所需的比例,请参阅seaborndocumentation中的更多内容。

例如,比例为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()

plot with big font scale

可以将matplotlib.axes.Axes.tick_params与labelsize一起使用。

例如,使用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()

enter image description here

我引用了以下答案:
 -Using matplotlib.colorbar.Colorbar object
 -Setting parameter

相关问题 更多 >