在plt.text(matplotlib.pyplot.text)中使用占位符(%d,%s) [英] Using placeholders (%d,%s) in plt.text (matplotlib.pyplot.text)
问题描述
我在使用 plt.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
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
我对 Python 还很陌生,因此不太了解如何在 Python 中使用占位符.我在网上搜索,但修复没有太大帮助.
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.
我的问题有两个:我可以使用 plt.text 下的占位符 (%d) 来调用包含所有手段的数组元素吗?如果是,那么如何?
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?
推荐答案
尝试以下操作:
for i, _ in enumerate(var):
plt.text(X_cord,Y_cord, r'$\mu=%s$' % (Mean[i]), fontsize=14)
我更喜欢使用字符串方法.format
:
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)
这篇关于在plt.text(matplotlib.pyplot.text)中使用占位符(%d,%s)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!