如何增加我的 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_context
:
http://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
.
我有以下代码来创建 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_context
:
http://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
.