0

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!

Srivatsan
  • 9,225
  • 13
  • 58
  • 83

1 Answers1

1

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.

Community
  • 1
  • 1
David Zwicker
  • 23,581
  • 6
  • 62
  • 77