Matplotlib中的MathText删除字符

时间:2018-06-07 15:17:43

标签: python matplotlib fonts

我正在尝试使用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')

1 个答案:

答案 0 :(得分:0)

如果使用的字符串不是有效的乳胶字符串,则它不能是有效的MathText字符串。

为了在乳胶数学模式中创建空间,您可以使用反斜杠 - 与MathText一样。

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

%标志是乳胶中的评论。与MathText相同。您需要转义注释符号才能将其用作符号:\%

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

enter image description here

但请注意,通过使文本完全加粗,您可以得到完全相同的输出,例如

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