直方图中的阴谋标签:保留原来的蜱字体 [英] Upright mu in plot label: retaining original tick fonts
问题描述
对于使用matplotlib.pyplot创建的图,我想把SI单位的微米纳入我的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)
结果如下所示:
微米和我想要的一样。但问题是x和y刻度的字体被改变了。这是因为:
matplotlib.rc('text',usetex = True)
如何将字体值重置为原始值?或者如何确保引入tex时字体不会改变?
作为参考,我所指的原始值如下所示:
除了尝试将字体还原为原始值之外,我还尝试了将微米合并到xlabel中的不同方法。这里出现的问题是,它仍然是斜体或它有一个大胆的类型设置。我正在寻找的微米是第一个数字给出的。
我希望有人能帮我解决这个问题。
提前发送
我也在努力解决这个问题,当text.usetex = True时,标签要一致。我设法找到的解决方案并不理想,但是暂时适用。
你需要做的是将字体系列设置为sans-serif,并添加一个使用无衬线数学字体的乳胶包(sfmath - make它是在你的tex路径!)
pre $ import matplotlib
import matplotlib.pyplot as plt
matplotlib.rc('text',usetex = True)
matplotlib.rc('font',** {'family':sans-serif})
params = {'text。 latex.preamble':[r'\usepackage {siunitx}',
r'\usepackage {sfmath}',r'\sisetup {detect-family = true}',
r' \ usepackage {amsmath}']}
plt.rcParams.update(params)
fig = plt.figure(figsize =(4,4))
ax = fig .add_subplot(1,1,1)
ax.set_xlabel('$ \si {\um} detection $')
ax.set_ylabel(r$ \mu \boldsymbol {\\ \\}
plt.show()
另外,我必须告诉siunitx包检测字体家族,我也不得不补充一些额外的文本到X标签为了使检测实际工作(您可以稍后删除此文本和标签将仍然工作之后)。
对我来说,这个结果是:
更一般地说,对于serif字体,我有以下我的〜/ .matplotlib / matploblibrc文件:
font.family:serif
text.latex.preamble:\ usepackage {mathptmx}
和sans-serif:
font.family:sans-serif
text.latex.preamble: \ usepackage {sfmath}
I have a problem which I thought would be more occurring. However, after scouring the internet for some time now I have not been able to find the solution to my problem. So here it goes:
For a plot, created using matplotlib.pyplot I want to incorporate the SI-unit micro meter into my xlabel. The unit micro meter however needs to be upright. After some fiddling around I achieved the desired xlabel.
The code that I have to generate this is:
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: 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)
How can I reset the font values to their original values? Or how can I make sure the fonts are not changed when introducing tex?
As a reference, the original values I am referring to look like this: 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.
I hope someone can help me solve this problem.
Thanx in advance
I am also struggling with such a problem, i.e. getting the tick labels and axes labels to be consistent when text.usetex = True. The solution that I have managed to find it not ideal, but it works for the moment.
What you have to do is set the font family to "sans-serif" and also add a latex package that uses sans-serif math fonts (sfmath -- make sure it is in your tex path!)
import matplotlib
import matplotlib.pyplot as plt
matplotlib.rc('text', usetex = True)
matplotlib.rc('font', **{'family':"sans-serif"})
params = {'text.latex.preamble': [r'\usepackage{siunitx}',
r'\usepackage{sfmath}', r'\sisetup{detect-family = true}',
r'\usepackage{amsmath}']}
plt.rcParams.update(params)
fig = plt.figure(figsize = (4,4))
ax = fig.add_subplot(1,1,1)
ax.set_xlabel('$\si{\um} detection$')
ax.set_ylabel(r"$\mu \boldsymbol{\mu}$")
plt.show()
In addition, I had to tell the siunitx package to detect the font family, and I also had to add some additional text to the x-label in order for the detection to actually work (you can remove this text later and the label will still work after that).
For me this results in: More generally, I have the following my ~/.matplotlib/matploblibrc file, for serif fonts:
font.family : serif
text.latex.preamble : \usepackage{mathptmx}
and for sans-serif:
font.family : sans-serif
text.latex.preamble : \usepackage{sfmath}
这篇关于直方图中的阴谋标签:保留原来的蜱字体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!