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

删除回忆录丶 提交于 2019-12-12 07:28:18

问题


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:


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)


来源:https://stackoverflow.com/questions/37799598/using-placeholders-d-s-in-plt-text-matplotlib-pyplot-text

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!