简体   繁体   English

同一个 Matplotlib 标签中的多种字体大小

[英]Multiple font sizes in same Matplotlib label

I'm trying to do something relatively simple:我正在尝试做一些相对简单的事情:

I want to be able to increase a font of one letter(say a LaTeX variable, say to 30) and keep the other letters in the label a certain font(say 20).我希望能够增加一个字母的字体(比如 LaTeX 变量,比如 30),并将标签中的其他字母保留为某种字体(比如 20)。

Does anyone have a quick solution?有没有人有一个快速的解决方案? It seems rather complicated to me.对我来说似乎相当复杂。 I tried using { } for each "item" in the label我尝试对标签中的每个“项目”使用{ }

plt.plot(a,b,'g',linewidth=3.5, label = 'a')
plt.plot(c,d,'r',linewidth=3.5, label = 'c')

plt.legend(labelspacing = 1.0,loc=1,prop={'size':40})

plt.xlabel({'a',fontsize=50},{ 'N',fontsize = 20})
plt.ylabel('%',fontsize =30)

Here is the solution with LaTeX.这是 LaTeX 的解决方案。 The machine I have doesn't have LaTeX installed, so I haven't tested this carefully.我的机器没有安装 LaTeX,所以我没有仔细测试过。

plt.plot(a,b,'g',linewidth=3.5, label = 'a')
plt.rc('text', usetex=True)
plt.legend(labelspacing = 1.0,loc=1,prop={'size':40})
plt.xlabel(r'{\fontsize{50pt}{3em}\selectfont{}a}{\fontsize{20pt}{3em}\selectfont{}N')

(note the r before the string. This tells pylab to just send the string directly to LaTeX as a raw string rather than treating \\f and \\s as special characters) (注意字符串前的r 。这告诉 pylab 将字符串作为原始字符串直接发送到 LaTeX,而不是将 \\f 和 \\s 视为特殊字符)

You can get much more elaborate with the size commands of LaTeX (you can specify the actual font, or use various versions of the \\large, \\Large, \\small \\tiny ... commands).您可以使用 LaTeX 的大小命令获得更详细的信息(您可以指定实际字体,或使用 \\large、\\Large、\\small \\tiny ... 命令的各种版本)。

One solution is to use text() and make multiple calls, carefully selecting where each letter goes:一种解决方案是使用 text() 并进行多次调用,仔细选择每个字母的位置:

import pylab as plt
a=[0,1]
b=[0,1]
plt.plot(a,b,'g',linewidth=3.5, label = 'a')
plt.rc('text', usetex=True)
plt.legend(labelspacing = 1.0,loc=1,prop={'size':40})

plt.text(0.45,-0.08,'a',fontsize=50)
plt.text(0.53,-0.08, 'N',fontsize = 20)

This isn't ideal.这并不理想。 Another option is to go through LaTeX.另一种选择是通过 LaTeX。 See other answer I'm about to post.查看我即将发布的其他答案。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM