Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change legend size with matplotlib.pyplot

Simple question here: I'm trying to get the size of my legend using matplotlib.pyplot to be smaller (i.e., the text to be smaller). The code I'm using goes something like this:

plot.figure() plot.scatter(k, sum_cf, color='black', label='Sum of Cause Fractions') plot.scatter(k, data[:, 0],  color='b', label='Dis 1: cf = .6, var = .2') plot.scatter(k, data[:, 1],  color='r',  label='Dis 2: cf = .2, var = .1') plot.scatter(k, data[:, 2],  color='g', label='Dis 3: cf = .1, var = .01') plot.legend(loc=2) 
like image 250
mike Avatar asked Aug 19 '11 17:08

mike


People also ask

How do you change the size of the legend?

To change the legend size of the plot, the user needs to use the cex argument of the legend function and specify its value with the user requirement, the values of cex greater than 1 will increase the legend size in the plot and the value of cex less than 1 will decrease the size of the legend in the plot.

How do I change the size of a figure in Pyplot?

If you've already got the figure created, say it's 'figure 1' (that's the default one when you're using pyplot), you can use figure(num=1, figsize=(8, 6), ...) to change it's size etc.


2 Answers

You can set an individual font size for the legend by adjusting the prop keyword.

plot.legend(loc=2, prop={'size': 6}) 

This takes a dictionary of keywords corresponding to matplotlib.font_manager.FontProperties properties. See the documentation for legend:

Keyword arguments:

prop: [ None | FontProperties | dict ]     A matplotlib.font_manager.FontProperties instance. If prop is a      dictionary, a new instance will be created with prop. If None, use     rc settings. 

It is also possible, as of version 1.2.1, to use the keyword fontsize.

like image 135
Yann Avatar answered Oct 04 '22 15:10

Yann


using import matplotlib.pyplot as plt

Method 1: specify the fontsize when calling legend (repetitive)

plt.legend(fontsize=20) # using a size in points plt.legend(fontsize="x-large") # using a named size 

With this method you can set the fontsize for each legend at creation (allowing you to have multiple legends with different fontsizes). However, you will have to type everything manually each time you create a legend.

(Note: @Mathias711 listed the available named fontsizes in his answer)

Method 2: specify the fontsize in rcParams (convenient)

plt.rc('legend',fontsize=20) # using a size in points plt.rc('legend',fontsize='medium') # using a named size 

With this method you set the default legend fontsize, and all legends will automatically use that unless you specify otherwise using method 1. This means you can set your legend fontsize at the beginning of your code, and not worry about setting it for each individual legend.

If you use a named size e.g. 'medium', then the legend text will scale with the global font.size in rcParams. To change font.size use plt.rc(font.size='medium')

like image 42
binnev Avatar answered Oct 04 '22 16:10

binnev