如何在注释中包含变量?

时间:2019-04-10 19:20:52

标签: python matplotlib

我已经在图形上添加了注释,仅添加文本就没有问题,但是我想在文本后添加变量的结果吗?

我尝试了一些放置变量的变体,但始终遇到语法错误。

pct_inc_m = (plotm11/plotm0-1) * 100

axs.annotate('The percentage increase for males for this period is (pct_inc_m'), xy=(0.01, 0.89), xycoords='axes fraction')

删除.annotate中的变量可使代码正常运行

1 个答案:

答案 0 :(得分:0)

由于引号不匹配,您收到了SyntaxError。

axs.annotate('The percentage increase for males for this period is (pct_inc_m)', xy=(0.01, 0.89), xycoords='axes fraction')
                                                                             ^^

尽管您可能希望显示变量的值:

axs.annotate('The percentage increase for males for this period is ({})'.format(pct_inc_m), xy=(0.01, 0.89), xycoords='axes fraction')