Saad.Akhtar312 Saad.Akhtar312 - 2 years ago 147
Python Question

Using placeholders (%d,%s) in plt.text (matplotlib.pyplot.text)

I am facing an issue in assigning a numerical value to the annotated text (mean and standard deviation in this case) using plt.text command.

Since I have six subplots, I want to automate this process instead of writing it one by one. The code snippet containing the problem is attached below:

# var=dictionary containing output variables
> for i in var:
> # Created Subplots for different vectors (includes labelling of axes) here

# Mean[] is an array containing mean values for all the plots

plt.text(X_cord,Y_cord, r'$\mu=$' %d, %d (Mean[b-1]), fontsize=14)
> #tick-properties
> # Setting limits for axes
> b+=1

I am fairly new to Python, therefore don't know much about how to use placeholders in Python. I searched through the web but the fixes did not help much.

My question is two folds: Can I use the placeholder (%d) under plt.text to call an element of an array containing all the means? If yes then how?

Answer Source

Try the following:

for i, _ in enumerate(var): 
    plt.text(X_cord,Y_cord, r'$\mu=%s$' % (Mean[i]), fontsize=14)

I prefer to use the string method.format:

for i, _ in enumerate(var): 
    plt.text(X_cord,Y_cord, r'$\mu={}$'.format(Mean[i]), fontsize=14)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download