根据the official guide,不再推荐使用pylab。 matplotlib.pyplot 应该直接使用。
通过rcParams 全局设置字体大小应该使用
import matplotlib.pyplot as plt
plt.rcParams['axes.labelsize'] = 16
plt.rcParams['axes.titlesize'] = 16
# or
params = {'axes.labelsize': 16,
'axes.titlesize': 16}
plt.rcParams.update(params)
# or
import matplotlib as mpl
mpl.rc('axes', labelsize=16, titlesize=16)
# or
axes = {'labelsize': 16,
'titlesize': 16}
mpl.rc('axes', **axes)
可以使用恢复默认值
plt.rcParams.update(plt.rcParamsDefault)
您也可以通过在matplotlib configuration directory 下的stylelib 目录中创建style sheet 来执行此操作(您可以从matplotlib.get_configdir() 获取您的配置目录)。样式表格式为
axes.labelsize: 16
axes.titlesize: 16
如果您在/path/to/mpl_configdir/stylelib/mystyle.mplstyle 有一个样式表,那么您可以通过
使用它
plt.style.use('mystyle')
# or, for a single section
with plt.style.context('mystyle'):
# ...
您还可以创建(或修改)matplotlibrc file,它共享格式
axes.labelsize = 16
axes.titlesize = 16
根据您修改的 matplotlibrc 文件,这些更改将仅用于当前工作目录、所有没有有 matplotlibrc 文件的工作目录,或所有有 的工作目录>没有有一个 matplotlibrc 文件,并且没有指定其他 matplotlibrc 文件。有关更多详细信息,请参阅自定义 matplotlib 页面的this section。
可以通过plt.rcParams.keys() 检索rcParams 键的完整列表,但您可以调整字体大小(斜体引自here)
-
axes.labelsize - x 和 y 标签的字体大小
-
axes.titlesize - 坐标区标题的字体大小
-
figure.titlesize - 图形标题的大小 (Figure.suptitle())
-
xtick.labelsize - 刻度标签的字体大小
-
ytick.labelsize - 刻度标签的字体大小
-
legend.fontsize - 图例的字体大小 (plt.legend(), fig.legend())
-
legend.title_fontsize - 图例标题的字体大小,None 设置为与默认轴相同。有关用法示例,请参阅 this answer。
所有这些都接受字符串大小{'xx-small', 'x-small', 'smaller', 'small', 'medium', 'large', 'larger', 'x-large', 'xxlarge'} 或pt 中的float。字符串大小是相对于由
指定的默认字体大小定义的
-
font.size - 文本的默认字体大小,以 pts 为单位。 10 pt 是标准值
另外,重量可以通过
-
font.weight - text.Text 使用的字体的默认粗细。接受 {100, 200, 300, 400, 500, 600, 700, 800, 900} 或 'normal' (400)、'bold' (700)、'lighter' 和 'bolder'(相对于当前重量)。