简体   繁体   English

使用Latex渲染时,如何在matplotlib图中更改轴刻度字体?

[英]How do I change the axis tick font in a matplotlib plot when rendering using Latex?

The font of the axis tick mark labels produced from the following code isn't Helvetica, but is still the default serif Computer Modern. 从以下代码生成的轴刻度标记的字体不是Helvetica,但仍然是默认的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()

I think the confusion here stems from the fact that you're mixing TeX and non-TeX font commands. 我认为这里的混乱源于你混合TeX和非TeX字体命令的事实。

This turns on TeX mode, so all of the text is rendered with an external TeX installation: 这将打开TeX模式,因此所有文本都使用外部TeX安装进行渲染:

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. 在这一行中,将其设置为sans-serif将传递给TeX,但TeX不能使用特定的ttf字体名称,因此忽略第二部分(涉及Helvetica)。 And setting the default body text in TeX does not (by default) change the math font. 在TeX中设置默认正文文本(默认情况下)不会更改数学字体。 This is (unfortunately standard TeX behavior). 这是(遗憾的是标准的TeX行为)。

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: 这会影响matplotlib的内部mathtext渲染器使用的字体集,并且对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: 我希望所有sans-serif出自TeX时使用的解决方案是使用cmbright包,可以通过添加以下内容打开:

rc('text.latex', preamble=r'\usepackage{cmbright}')

That may require installing the cmbright LaTeX package if you don't already have it. 这可能需要安装cmbright LaTeX软件包(如果您还没有)。

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. 奇怪的。

I just found a little simple way you can try. 我发现你可以尝试一些简单的方法。 Just call the .xticks method and give the fontname as user-set argument. 只需调用.xticks方法并将fontname作为用户设置参数。

eg 例如

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在Matplotlib中使用LaTex渲染轴标签 - Rendering of axis label using LaTex in Matplotlib Matplotlib + Latex Rendering / twinx():次要Y轴上的字体错误? - Matplotlib + Latex Rendering / twinx(): wrong font on secondary y-axis? 如何使用matplotlib在动态tkinter图表中更改轴刻度线标签 - How to Change Axis Tick Labels in dynamic tkinter chart using matplotlib 如何在 Matplotlib 中更改轴刻度 label 速率 - How to change axis tick label rate in Matplotlib 如何在matplotlib中使用Latex在我的轴标题中获得每千米标志? - How do I get a per mille sign in my axis title using Latex in matplotlib? 在 Python matplotlib 中使用 Times New Roman 字体激活 LaTeX 文本渲染时,如何获得 xticklabel 和 yticklabel 粗体? - How can I get xticklabel and yticklabel bold when activating LaTeX text rendering with Times New Roman font in Python matplotlib? 如何更改matplotlib中的默认乳胶字体 - How to change the default latex font in matplotlib 如何在.py文件中使用Matplotlib在图表的图例中编写Latex公式? - How do I write a Latex formula in the legend of a plot using Matplotlib inside a .py file? 如何使用 Matplotlib 更改 Y 轴的刻度距离? - How do I change the scale distance in the Y axis using Matplotlib? 使用 matplotlib 和 Latex 时不同的字体粗细 - Different font weight when using matplotlib with latex
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM