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()
Matplotlib can use LaTeX to render text. This is activated by setting text. usetex : True in your rcParams, or by setting the usetex property to True on individual Text objects.
To set labels on the x-axis and y-axis, use the plt. xlabel() and plt. ylabel() methods.
To set the color for X-axis and Y-axis, we can use the set_color() method (Set both the edgecolor and the facecolor). To set the ticks color, use tick_params method for axes. Used arguments are axis ='x' (or y or both) and color = 'red' (or green or yellow or ...etc.)
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With