简体   繁体   中英

Plotting a calculated value in pyplot

I have calculated few values, say p1=3.5 and p2=4.5 (I am only giving sample values here). They both are numpy objects. Now I need to use these values in my pyplot.

I tried

plt.text(0.0,0.0,'c for 13.6<M_{200}<13.7 =',p1)

but I get an error saying AttributeError: 'numpy.float64' object has no attribute 'items'

I am using Python 2.6 and I have also tried it with plt.figtext , but I am not able to use the calculated values.

I know that I can rather say plt.text(0.0,0.0,'c for 13.6<M_{200}<13.7 =3.5') which will give me the desired output, but I need to do this for many values!

You have to create the full string beforehand

plt.text(0.0, 0.0, 'c for 13.6<M_{200}<13.7 = %g' % p1)

Here, I used the old string formatting syntax to avoid escaping the brackets. With the new string formatting syntax , the same line would read

plt.text(0.0, 0.0, 'c for 13.6<M_{{200}}<13.7 = {}'.format(p1))

There also is an interesting thread on SO on string formatting .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM