小编pri*_*llo的帖子

matplotlib Python 中的“%”符号

有什么方法可以在文本对象或图例中使用 matplotlib 在 python 的绘图中打印 % 符号?

这个基本示例没有原始百分号,其行为符合预期:

plt.figure()
TotEne,= plt.plot_date(time, ETTG['output'], 'b-', label='Total Energy')
UsEne,= plt.plot_date(time, EUUG['output'], 'r-',label='Used Energy')
plt.ylabel('kWh')
plt.text('2015-12-15',25000,'Energy Losses=%.2f'%pEW, clip_on=True)
plt.legend(handles=[TotEne,UsEne])   
plt.show()
Run Code Online (Sandbox Code Playgroud)

在这种情况下,文本显示“能量损失=”。但我想知道是否有可能显示“能量损失=%”或“%能量损失=”。

当我尝试这样做时,它会引发类型错误。我只想按字面意思显示“%”字符。所以这样做:

plt.figure()
TotEne,= plt.plot_date(time, ETTG['output'], 'b-', label='Total Energy')
UsEne,= plt.plot_date(time, EUUG['output'], 'r-',label='Used Energy')
plt.ylabel('kWh')
plt.text('2015-12-15',25000,'%Energy Losses=%.2f'%pEW, clip_on=True)
plt.legend(handles=[TotEne,UsEne])  
plt.show()
Run Code Online (Sandbox Code Playgroud)

引发此错误:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-559-9d13c78a6754> in <module>()
     20 plt.ylabel('kWh')
     21 
---> 22 plt.text('2015-12-15',25000,'%Energy Losses=%.2f'%pEW, clip_on=True)
     23 
     24 plt.legend(handles=[TotEne,UsEne])

TypeError: not enough arguments for format string
Run Code Online (Sandbox Code Playgroud)

如何使用运算符在带有插值字符串的 matplotlib 图形中包含文字百分号(“%”)%

python plot text matplotlib legend

3
推荐指数
1
解决办法
3476
查看次数

标签 统计

legend ×1

matplotlib ×1

plot ×1

python ×1

text ×1