将带有下标的str(变量)添加到matplotlib标签

时间:2020-05-11 13:59:39

标签: python-3.x matplotlib

我想在我的情节中添加这样的标签:

plt.plot(self.x,y,label ='$ L _ {} $'。format(str(component)))

但是,对于component = 10、11、12,结果如图所示。.只有组件的第一个字符才打印在下标...

enter image description here

请提出解决方案。

1 个答案:

答案 0 :(得分:0)

您需要使用双{{}}大括号,并且也不需要转换为字符串。有关更多说明,请参见this答案。

plt.plot(self.x, y, label = '$L_{{{}}}$'.format(component))