繁体   English   中英

如何在我的Seaborn图中增加图例的字体大小?

[英]How to increase the font size of the legend in my Seaborn plot?

我有以下代码来创建Seaborn条带图。 我很难弄清楚如何增加绘图中出现的图例的字体大小。

g=sns.stripplot(x="Market", y="Rate", hue="Group",data=myBenchmarkData, jitter=True, size=12, alpha=0.5)
g.axes.set_title("4* Rate Market and by Hotel Groups for Year 2016",fontsize=25)
g.set_xlabel("Market",fontsize=20)
g.set_ylabel("Rate (in EUR)",fontsize=20)
g.tick_params(labelsize=15)
plt.savefig ('benchmark1.png')

我的x轴和y轴标签字体大小没问题,但我的绘图中图例的字体大小很小。 怎么改呢?

根据此示例使用matplotlib函数setp

import seaborn as sns
import matplotlib.pylab as plt
sns.set_style("whitegrid")
tips = sns.load_dataset("tips")

ax = sns.stripplot(x="sex", y="total_bill", hue="day", data=tips, jitter=True)
plt.setp(ax.get_legend().get_texts(), fontsize='22') # for legend text
plt.setp(ax.get_legend().get_title(), fontsize='32') # for legend title

plt.show()

在此输入图像描述

另一种方法是改变font_scale所有图形与plotting_contexthttp://seaborn.pydata.org/generated/seaborn.plotting_context.html

今天有一种更容易的方法,只需设置你的数字,然后打电话

plt.legend(fontsize='x-large', title_fontsize='40')

https://matplotlib.org/api/_as_gen/matplotlib.pyplot.legend.html

可能取决于您正在使用的matplotlib的版本。 我使用2.2.3并且它有fontsize参数但不是title_fontsize

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM