Inconsistent tick labels font with matplotlib subplots

我的梦境 提交于 2019-12-20 04:09:45

问题


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.

Here's a 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()

If you comment out the fig, axes = plt.subplots line, the tick labels display as they should.

I'm using python version 3.6.0 and matplotlib version 2.0.0


回答1:


Changes to the rcParams should always be made as soon as possible and necessarily before the objects they affect are created.

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()


来源:https://stackoverflow.com/questions/42346562/inconsistent-tick-labels-font-with-matplotlib-subplots

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!