Python 在seaborn heatmap中自动调整字体大小
当使用seaborn heatmap时,有没有一种方法可以自动调整字体大小,使其正好适合正方形内部? 例如:Python 在seaborn heatmap中自动调整字体大小,python,matplotlib,seaborn,Python,Matplotlib,Seaborn,当使用seaborn heatmap时,有没有一种方法可以自动调整字体大小,使其正好适合正方形内部? 例如: sns.heatmap(corrmat, vmin=corrmat.values.min(), vmax=1, square=True, cmap="YlGnBu", linewidths=0.1, annot=True, annot_kws={"size":8}) 在这里,尺寸以“annot_kws”设置 尽管它会扭曲热图,但此示例演示了如何使用.set(…)上
sns.heatmap(corrmat, vmin=corrmat.values.min(), vmax=1, square=True, cmap="YlGnBu",
linewidths=0.1, annot=True, annot_kws={"size":8})
在这里,尺寸以“annot_kws”设置 尽管它会扭曲热图,但此示例演示了如何使用
.set(…)
上下文缩放字体
import matplotlib.pyplot as plt
import seaborn as sns
sns.set(font_scale=3)
# Load the example flights dataset and conver to long-form
flights_long = sns.load_dataset("flights")
flights = flights_long.pivot("month", "year", "passengers")
# Draw a heatmap with the numeric values in each cell
f, ax = plt.subplots(figsize=(9, 6))
sns.heatmap(flights, annot=True, fmt="d", linewidths=.5, ax=ax)
f.savefig("output.png")
您还可以执行以下操作:
sns.heatmap(corrmat, vmin=corrmat.values.min(), vmax=1, square=True, cmap="YlGnBu", linewidths=0.1, annot=True, annot_kws={"fontsize":8})
要调整的字体大小,有不同的方法
import seaborn as sns # for data visualization
flight = sns.load_dataset('flights') # load flights datset from GitHub seaborn repository
# reshape flights dataeset in proper format to create seaborn heatmap
flights_df = flight.pivot('month', 'year', 'passengers')
sns.heatmap(flights_df) # create seaborn heatmap
sns.set(font_scale=2) # font size 2
输出>>>
所有seaborn图形标签的sns.set(font_scale=2)#font size 2
设置大小
这就是为什么如果你愿意,可以采用另一种方法
import seaborn as sns # for data visualization
import matplotlib.pyplot as plt # for data visualization
flight = sns.load_dataset('flights') # load flights datset from GitHub seaborn repository
# reshape flights dataeset in proper format to create seaborn heatmap
flights_df = flight.pivot('month', 'year', 'passengers')
sns.heatmap(flights_df) # create seaborn heatmap
plt.title('Heatmap of Flighr Dataset', fontsize = 20) # title with fontsize 20
plt.xlabel('Years', fontsize = 15) # x-axis label with fontsize 15
plt.ylabel('Monthes', fontsize = 15) # y-axis label with fontsize 15
plt.show()
输出>>>
如果您想要自动的东西,这还不错:
annot_kws={"size": 35 / np.sqrt(len(corrmat))},
没有;这取决于太多无法可靠预测的因素。感谢
annot_kws={“size”:8}
!正是我想要的:)。