问题
The font of the axis tick mark labels produced from the following code isn't Helvetica, but is still the default serif Computer Modern. Any suggestions are greatly appreciated.
from matplotlib import rc, font_manager
from numpy import arange, cos, pi
from matplotlib.pyplot import figure, axes, plot, xlabel, ylabel, title, \
grid, savefig, show
sizeOfFont = 12
fontProperties = {'family':'sans-serif','sans-serif':['Helvetica'],
'weight' : 'normal', 'size' : sizeOfFont}
ticks_font = font_manager.FontProperties(family='Helvetica', style='normal',
size=sizeOfFont, weight='normal', stretch='normal')
rc('text', usetex=True)
rc('font',**fontProperties)
figure(1, figsize=(6,4))
ax = axes([0.1, 0.1, 0.8, 0.7])
t = arange(0.0, 1.0+0.01, 0.01)
s = cos(2*2*pi*t)+2
plot(t, s)
for label in ax.get_xticklabels():
label.set_fontproperties(ticks_font)
for label in ax.get_yticklabels():
label.set_fontproperties(ticks_font)
xlabel(r'\textbf{time (s)}')
ylabel(r'\textit{voltage (mV)}',fontsize=16,family='Helvetica')
title(r"\TeX\ is Number $\displaystyle\sum_{n=1}^\infty\frac{-e^{i\pi}}{2^n}$!",
fontsize=16, color='r')
grid(True)
savefig('tex_demo.pdf')
show()
回答1:
Okay, this worked for me. Replace the following lines:
for label in ax.get_xticklabels():
label.set_fontproperties(ticks_font)
for label in ax.get_yticklabels():
label.set_fontproperties(ticks_font)
with this:
from matplotlib.pyplot import gca
a = gca()
a.set_xticklabels(a.get_xticks(), fontProperties)
a.set_yticklabels(a.get_yticks(), fontProperties)
What you did in your original code makes sense to me, but I get different results this way. Weird.
回答2:
I think the confusion here stems from the fact that you're mixing TeX and non-TeX font commands.
This turns on TeX mode, so all of the text is rendered with an external TeX installation:
rc('text', usetex=True)
In this line, setting it to sans-serif will get passed along to TeX, but a specific ttf font name can not be used by TeX, so the second part (involving Helvetica) is ignored. And setting the default body text in TeX does not (by default) change the math font. This is (unfortunately standard TeX behavior).
rc('font',**{'family':'sans-serif','sans-serif':['Helvetica']})
This affects the font set used by matplotlib's internal mathtext renderer, and has no effect on TeX:
rc('mathtext', fontset='stixsans')
The solution I use when I want all sans-serif out of TeX is to use the cmbright package, which can be turned on by adding:
rc('text.latex', preamble=r'\usepackage{cmbright}')
That may require installing the cmbright LaTeX package if you don't already have it.
回答3:
I just found a little simple way you can try. Just call the .xticks method and give the fontname as user-set argument.
e.g.
import matplotlib.pyplot as plt
plt.figure()
#... do the plot you want...
plt.yticks(fontname = "Times New Roman") # This argument will change the font.
plt.show()
来源:https://stackoverflow.com/questions/12322738/how-do-i-change-the-axis-tick-font-in-a-matplotlib-plot-when-rendering-using-lat