Skip to content Skip to sidebar Skip to footer

Pass Value In The Text Of Plt.text Function

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_inche

Solution 1:

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)

Post a Comment for "Pass Value In The Text Of Plt.text Function"