Reputation: 4764
Here is the abstracted problem, every time lst
is different, so I want to do it automatically
import matplotlib.pyplot as plt
import numpy as np
fig=plt.figure()
fig.set_size_inches(8,4)
lst=np.array ([0.1235,0.2322,0.3300])
#how can I directly pass these value to plt.text, without manually input
plt.text(0.05,0.6,r'a=0.124, b=0.232, c=0.330',fontsize=18)
plt.show()
Upvotes: 0
Views: 5270
Reputation: 90879
You can use str.format()
and pass in the list as the parameter to it (and use indexes inside {}
to determine where and with what format a element goes , Example -
plt.text(0.05,0.6,r'a={0:.3f}, b={1:.3f}, c={2:.3f}'.format(*lst),fontsize=18)
The number before :
is the index of the element which needs to be substituted there, and the .3f
denotes it will have 3 places after decimal , and f is for float.
Please note, above will not round the number (a would be printed with - a=0.123
) , if you want the number rounded as well , you cna use list comprehension with round function -
plt.text(0.05,0.6,r'a={0:.3f}, b={1:.3f}, c={2:.3f}'.format(*[round(x,3) for x in lst]),fontsize=18)
Upvotes: 6