问题描述
我正在使用 matplotlib.pyplot.subplots
绘制图表网格,我希望刻度标签使用LaTeX的sans-serif字体,但是当我使用subplots时,我总能得到至少一个刻度标签以 matplotlib 的默认字体呈现.
I'm making a grid of plots using matplotlib.pyplot.subplots
, and I want the tick labels to be in LaTeX's sans-serif font, but when I do use subplots I always get at least one tick label rendered in matplotlib's default font.
这是MWE:
import matplotlib.pyplot as plt
fig, axes = plt.subplots(nrows=1, ncols=1)
x = [1,2,3,4,5]
plt.plot(x)
plt.rc('text', usetex=True)
plt.rc('font', family='sans-serif')
plt.show()
如果您注释掉 fig, axes = plt.subplots
行,刻度标签会按原样显示.
If you comment out the fig, axes = plt.subplots
line, the tick labels display as they should.
我正在使用python 3.6.0版和matplotlib 2.0.0版
I'm using python version 3.6.0 and matplotlib version 2.0.0
推荐答案
对 rcParams 的更改应始终尽快进行,并且必须在创建它们影响的对象之前进行.
Changes to the rcParams should always be made as soon as possible and necessarily before the objects they affect are created.
因此,将rc更改放在最前面,可以解决此问题:
Thus, putting the rc changes at the top, resolves the issue:
import matplotlib.pyplot as plt
plt.rc('text', usetex=True)
plt.rc('font', family='sans-serif')
fig, axes = plt.subplots(nrows=1, ncols=1)
x = [1,2,3,4,5]
plt.plot(x)
plt.show()
这篇关于matplotlib子图的刻度标签字体不一致的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!