Matplotlib中的MathText删除字符

2024-04-20 00:17:54 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试使用MathText在我的图例中加粗特定的字符串。单词是粗体的,但MathText不断删除特定字符。如何让MathText停止删除特定字符?我正在使用的字符串被附加到列表中,并在图例语句中使用。你知道吗

例如:

nameedit="Bacillus cereus"

#keeps removing the space
r"$\bf{" +nameedit+ r"}$"

#keeps removing the space
r"$\bf" +nameedit+ r"$"

#space removed and %) removed
r"$\bf" +nameedit+' ('+str(round(70/100*100))+'%)'+ r"$"

#does not print anything
r"$\bf{" +nameedit+' ('+str(round(70/100*100))+'%)'+ r"}$"

#Here is the legend statement
ax.legend(circlist,namelegend,bbox_to_anchor=(1,1),loc=2,fontsize=3.3,title='Top Ten Abundance')

Tags: the字符串space字符单词图例legendremoving
1条回答
网友
1楼 · 发布于 2024-04-20 00:17:54

如果正在使用的字符串不是有效的latex字符串,则它不能是有效的MathText字符串。你知道吗

为了在latex数学模式下创建空间,可以使用反斜杠-MathText也一样。你知道吗

r"$\bf{Bacillus\ cereus}$"

%符号是latex中的注释。与MathText相同。您需要转义注释符号以将其用作符号:\%

r"$\bf{Bacillus\ cereus\ (70\%)}$"

enter image description here

但是请注意,如果将文本全部加粗,则会得到完全相同的输出

plt.title("Bacillus cereus (70%)", weight="bold")

相关问题 更多 >