Inserting a previously calculated value in a legend in matplotlib

半腔热情 提交于 2019-12-31 05:01:06

问题


This should be simple, but I haven't found it: I have a value (the coefficient R squared) previously calculated and I want to insert it in a legend in matplotlib. If the value calculated is r_squared, this is not working:

plt.plot(x, y, linestyle='solid', color='k', label=r'Fitting line with $R^{2} = $, r_squared')

It just don't read the value.

Thanks.


回答1:


You could just use string formatting :

plt.plot(x, y, linestyle='solid', color='k', label="Fitting line with $R^{{2}} = {}".format(r_squared))


来源:https://stackoverflow.com/questions/41674696/inserting-a-previously-calculated-value-in-a-legend-in-matplotlib

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!