绘图标签中的 Upright mu:保留原始刻度字体

2023-11-26

我有一个我认为会更频繁发生的问题。然而,在互联网上搜索了一段时间后,我无法找到解决我的问题的方法。所以事情是这样的:

对于使用 matplotlib.pyplot 创建的绘图,我想将 SI 单位微米纳入我的 xlabel 中。然而,单位微米需要是直立的。经过一番摆弄后,我获得了所需的 xlabel。

我必须生成的代码是:

import matplotlib
import matplotlib.pyplot as plt

matplotlib.rc('text', usetex = True)
params = {'text.latex.preamble': [r'\usepackage{siunitx}', r'\usepackage{cmbright}']}
plt.rcParams.update(params)


fig = plt.figure()
ax = fig.add_subplot(1,1,1)

ax.set_xlabel('$\si{\micro \meter}$', fontsize = 16)
ax.set_ylabel("hi", fontsize = 16)

plt.savefig("test.png")

The result is shown below: enter image description here The micro meter is exactly as I want it to be. The problem however is that the font of the x and y ticks is altered. This is because of:

matplotlib.rc('text', usetex = True)

如何将字体值重置为原始值?或者如何确保引入tex时字体不改变?

As a reference, the original values I am referring to look like this: enter image description here Besides trying to revert the fonts back to their original values I also tried different methods of incorporating micro meter into my xlabel. The problem that arises here is that I it remains italic or it has a bold type setting. The micro meter I am looking for is the one given in the first figure.

我希望有人能帮助我解决这个问题。

提前感谢


对我有用的是不usetex,但要使用 Unicode:

ax.set_xlabel(u'\u03bc')

将标签设置为单个直立标签mu.

加载时需要进行以下设置matplotlib:

import matplotlib
matplotlib.rcParams['mathtext.fontset'] = 'cm'
matplotlib.rc('font', family='serif', serif='CMU Serif')
import matplotlib.pyplot as plt

在这里,我使用 Sourceforge 中的“Computer Modern Unicode”字体(如果您希望与 LaTeX 中的书写排版及其默认的 Computer Modern 字体保持一致,强烈推荐)。

但是任何带有mu字形应该可以工作。事实上,mu来自 CMU Serif 的美学效果不如mu来自 SIunitx,但它是正确的。

需要重新启动 Python 才能生效。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

绘图标签中的 Upright mu:保留原始刻度字体 的相关文章

随机推荐