Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

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?

like image 828
user3115933 Avatar asked Jul 03 '17 07:07

user3115933


People also ask

How do I change font size in legend in Seaborn?

This is one of the easiest methods to change the font size of any Seaborn legends, in this we just have to pass the parameter of the fontsize which allows us to pass the font-size value and it will change the font size.

How do I change the font size in legend?

Direct link to this answer You can change the font size for a MATLAB legend by setting the 'FontSize' property of the Legend object. For example, plot four lines. Create a legend and assign the Legend object to the variable 'lgd'. Then, use dot notation to access the 'FontSize' property and set the value to 14 points.

How do I increase the legend font in MatPlotLib?

MatPlotLib with Python Title the plot using the title() method. To set the fontsize, we can override rcParams legend fontsize by value 20. Use the legend() method, and fit the legend at the top-right position. To display the figure, use the show() method.


2 Answers

Use matplotlib function setp according to this example:

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_scale of all graph with plotting_context: http://seaborn.pydata.org/generated/seaborn.plotting_context.html

like image 97
Serenity Avatar answered Sep 27 '22 00:09

Serenity


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

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

like image 30
Major Major Avatar answered Sep 26 '22 00:09

Major Major