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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/44880444/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 00:33:46  来源:igfitidea点击:

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

pythonmatplotliblegendseaborn

提问by user3115933

I have the following codes to create a Seaborn strip plot. I am having a hard time figuring out how to increase the font size of the legend appearing in the 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')

I am OK with my x-axis and y-axis labels font size but the font size of the legend in my plot is small. How to change it?

我的 x 轴和 y 轴标签字体大小没问题,但我的图中图例的字体大小很小。如何改变它?

回答by Serenity

Use matplotlib function setpaccording to this example:

setp根据此示例使用 matplotlib 函数:

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

enter image description here

在此处输入图片说明

Another way is to change font_scaleof all graph with plotting_context: http://seaborn.pydata.org/generated/seaborn.plotting_context.html

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

回答by Major Major

There is a much easier way to do this today, simply set up your figure and then call

今天有一个更简单的方法来做到这一点,只需设置你的数字,然后调用

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

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

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

Might depend on the version of matplotlib you're using. I'm using 2.2.3 and it has the fontsizeparameter but not the title_fontsize.

可能取决于您使用的 matplotlib 版本。我使用的是 2.2.3,它有fontsize参数但没有title_fontsize.