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

2019-03-06 01:31发布

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
  b=1
> 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?

1条回答
做自己的国王
2楼-- · 2019-03-06 02:04

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)
查看更多
登录 后发表回答