import matplotlib.pyplot as plt
SMALL_SIZE =8
MEDIUM_SIZE =10
BIGGER_SIZE =12
plt.rc('font', size=SMALL_SIZE)# controls default text sizes
plt.rc('axes', titlesize=SMALL_SIZE)# fontsize of the axes title
plt.rc('axes', labelsize=MEDIUM_SIZE)# fontsize of the x and y labels
plt.rc('xtick', labelsize=SMALL_SIZE)# fontsize of the tick labels
plt.rc('ytick', labelsize=SMALL_SIZE)# fontsize of the tick labels
plt.rc('legend', fontsize=SMALL_SIZE)# legend fontsize
plt.rc('figure', titlesize=BIGGER_SIZE)# fontsize of the figure title
请注意,您还可以设置在以下位置调用rc方法的大小matplotlib:
import matplotlib
SMALL_SIZE =8
matplotlib.rc('font', size=SMALL_SIZE)
matplotlib.rc('axes', titlesize=SMALL_SIZE)# and so on ...
If you are a control freak like me, you may want to explicitly set all your font sizes:
import matplotlib.pyplot as plt
SMALL_SIZE = 8
MEDIUM_SIZE = 10
BIGGER_SIZE = 12
plt.rc('font', size=SMALL_SIZE) # controls default text sizes
plt.rc('axes', titlesize=SMALL_SIZE) # fontsize of the axes title
plt.rc('axes', labelsize=MEDIUM_SIZE) # fontsize of the x and y labels
plt.rc('xtick', labelsize=SMALL_SIZE) # fontsize of the tick labels
plt.rc('ytick', labelsize=SMALL_SIZE) # fontsize of the tick labels
plt.rc('legend', fontsize=SMALL_SIZE) # legend fontsize
plt.rc('figure', titlesize=BIGGER_SIZE) # fontsize of the figure title
Note that you can also set the sizes calling the rc method on matplotlib:
import matplotlib
SMALL_SIZE = 8
matplotlib.rc('font', size=SMALL_SIZE)
matplotlib.rc('axes', titlesize=SMALL_SIZE)
# and so on ...
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.font_manager as font_manager
# Set the font dictionaries (for plot title and axis titles)
title_font ={'fontname':'Arial','size':'16','color':'black','weight':'normal','verticalalignment':'bottom'}# Bottom vertical alignment for more space
axis_font ={'fontname':'Arial','size':'14'}# Set the font properties (for use in legend)
font_path ='C:\Windows\Fonts\Arial.ttf'
font_prop = font_manager.FontProperties(fname=font_path, size=14)
ax = plt.subplot()# Defines ax variable by creating an empty plot# Set the tick labels fontfor label in(ax.get_xticklabels()+ ax.get_yticklabels()):
label.set_fontname('Arial')
label.set_fontsize(13)
x = np.linspace(0,10)
y = x + np.random.normal(x)# Just simulates some data
plt.plot(x, y,'b+', label='Data points')
plt.xlabel("x axis",**axis_font)
plt.ylabel("y axis",**axis_font)
plt.title("Misc graph",**title_font)
plt.legend(loc='lower right', prop=font_prop, numpoints=1)
plt.text(0,0,"Misc text",**title_font)
plt.show()
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.font_manager as font_manager
import matplotlib.ticker
# Workaround for Matplotlib 2.0.0 log axes bug https://github.com/matplotlib/matplotlib/issues/8017 :
matplotlib.ticker._mathdefault =lambda x:'\\mathdefault{%s}'%x
# Set the font properties (can use more variables for more fonts)
font_path ='C:\Windows\Fonts\AGaramondPro-Regular.otf'
font_prop = font_manager.FontProperties(fname=font_path, size=14)
ax = plt.subplot()# Defines ax variable by creating an empty plot# Define the data to be plotted
x = np.linspace(0,10)
y = x + np.random.normal(x)
plt.plot(x, y,'b+', label='Data points')for label in(ax.get_xticklabels()+ ax.get_yticklabels()):
label.set_fontproperties(font_prop)
label.set_fontsize(13)# Size here overrides font_prop
plt.title("Exponentially decaying oscillations", fontproperties=font_prop,
size=16, verticalalignment='bottom')# Size here overrides font_prop
plt.xlabel("Time", fontproperties=font_prop)
plt.ylabel("Amplitude", fontproperties=font_prop)
plt.text(0,0,"Misc text", fontproperties=font_prop)
lgd = plt.legend(loc='lower right', prop=font_prop)# NB different 'prop' argument for legend
lgd.set_title("Legend", prop=font_prop)
plt.show()
Update: See the bottom of the answer for a slightly better way of doing it. Update #2: I’ve figured out changing legend title fonts too. Update #3: There is a bug in Matplotlib 2.0.0 that’s causing tick labels for logarithmic axes to revert to the default font. Should be fixed in 2.0.1 but I’ve included the workaround in the 2nd part of the answer.
This answer is for anyone trying to change all the fonts, including for the legend, and for anyone trying to use different fonts and sizes for each thing. It does not use rc (which doesn’t seem to work for me). It is rather cumbersome but I could not get to grips with any other method personally. It basically combines ryggyr’s answer here with other answers on SO.
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.font_manager as font_manager
# Set the font dictionaries (for plot title and axis titles)
title_font = {'fontname':'Arial', 'size':'16', 'color':'black', 'weight':'normal',
'verticalalignment':'bottom'} # Bottom vertical alignment for more space
axis_font = {'fontname':'Arial', 'size':'14'}
# Set the font properties (for use in legend)
font_path = 'C:\Windows\Fonts\Arial.ttf'
font_prop = font_manager.FontProperties(fname=font_path, size=14)
ax = plt.subplot() # Defines ax variable by creating an empty plot
# Set the tick labels font
for label in (ax.get_xticklabels() + ax.get_yticklabels()):
label.set_fontname('Arial')
label.set_fontsize(13)
x = np.linspace(0, 10)
y = x + np.random.normal(x) # Just simulates some data
plt.plot(x, y, 'b+', label='Data points')
plt.xlabel("x axis", **axis_font)
plt.ylabel("y axis", **axis_font)
plt.title("Misc graph", **title_font)
plt.legend(loc='lower right', prop=font_prop, numpoints=1)
plt.text(0, 0, "Misc text", **title_font)
plt.show()
The benefit of this method is that, by having several font dictionaries, you can choose different fonts/sizes/weights/colours for the various titles, choose the font for the tick labels, and choose the font for the legend, all independently.
UPDATE:
I have worked out a slightly different, less cluttered approach that does away with font dictionaries, and allows any font on your system, even .otf fonts. To have separate fonts for each thing, just write more font_path and font_prop like variables.
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.font_manager as font_manager
import matplotlib.ticker
# Workaround for Matplotlib 2.0.0 log axes bug https://github.com/matplotlib/matplotlib/issues/8017 :
matplotlib.ticker._mathdefault = lambda x: '\\mathdefault{%s}'%x
# Set the font properties (can use more variables for more fonts)
font_path = 'C:\Windows\Fonts\AGaramondPro-Regular.otf'
font_prop = font_manager.FontProperties(fname=font_path, size=14)
ax = plt.subplot() # Defines ax variable by creating an empty plot
# Define the data to be plotted
x = np.linspace(0, 10)
y = x + np.random.normal(x)
plt.plot(x, y, 'b+', label='Data points')
for label in (ax.get_xticklabels() + ax.get_yticklabels()):
label.set_fontproperties(font_prop)
label.set_fontsize(13) # Size here overrides font_prop
plt.title("Exponentially decaying oscillations", fontproperties=font_prop,
size=16, verticalalignment='bottom') # Size here overrides font_prop
plt.xlabel("Time", fontproperties=font_prop)
plt.ylabel("Amplitude", fontproperties=font_prop)
plt.text(0, 0, "Misc text", fontproperties=font_prop)
lgd = plt.legend(loc='lower right', prop=font_prop) # NB different 'prop' argument for legend
lgd.set_title("Legend", prop=font_prop)
plt.show()
The smaller you make the figure size, the larger the font is relative to the plot. This also upscales the markers. Note I also set the dpi or dot per inch. I learned this from a posting the AMTA (American Modeling Teacher of America) forum.
Example from above code:
You can use plt.rcParams["font.size"] for setting font_size in matplotlib and also you can use plt.rcParams["font.family"] for setting font_family in matplotlib. Try this example:
This is an extension to Marius Retegan answer. You can make a separate JSON file with all your modifications and than load it with rcParams.update. The changes will only apply to the current script. So
import json
from matplotlib import pyplot as plt, rcParams
s = json.load(open("example_file.json")
rcParams.update(s)
and save this ‘example_file.json’ in the same folder.
I totally agree with Prof Huster that the simplest way to proceed is to change the size of the figure, which allows keeping the default fonts. I just had to complement this with a bbox_inches option when saving the figure as a pdf because the axis labels were cut.
import matplotlib.pyplot as plt
plt.figure(figsize=(4,3))
plt.savefig('Basic.pdf', bbox_inches='tight')