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)
On my install, FontProperties only changes the text size, but it's still too large and spaced out. I found a parameter in
pyplot.rcParams
:legend.labelspacing
, which I'm guessing is set to a fraction of the font size. I've changed it withI'm not sure how to specify it to the pyplot.legend function - passing
or
comes back with an error.
This should do
Then do the plot afterwards.
There are a ton of other rcParams, they can also be set in the matplotlibrc file.
Also presumably you can change it passing a
matplotlib.font_manager.FontProperties
instance but this I don't know how to do. --> see Yann's answer.using
import matplotlib.pyplot as plt
Method 1: specify the fontsize when calling legend (repetitive)
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)
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 globalfont.size
inrcParams
. To changefont.size
useplt.rc(font.size='medium')
You can set an individual font size for the legend by adjusting the
prop
keyword.This takes a dictionary of keywords corresponding to
matplotlib.font_manager.FontProperties
properties. See the documentation for legend:It is also possible, as of version 1.2.1, to use the keyword
fontsize
.There are multiple settings for adjusting the legend size. The two I find most useful are:
legend(..., labelspacing=0.2)
will reduce the spacing between entries to 2 points. The default on my install is about 0.5.legend(..., prop={'size':8})
. The default on my install is about 14 points.In addition, the legend documentation lists a number of other padding and spacing parameters including:
borderpad
,handlelength
,handletextpad
,borderaxespad
, andcolumnspacing
. These all follow the same form as labelspacing and area also in multiples of fontsize.These values can also be set as the defaults for all figures using the matplotlibrc file.
There are also a few named fontsizes, apart from the size in points:
Usage: