[英]Python variable names through a for loop
在Matplotlib中,我使用以下命令設置軸標簽:axes1.set_ylabel('....')
如果我有三個軸,是否可以使用循環執行以下操作?
for i in range(3):
axes[i].set_ylabel('AVG')
當然,這不是正確的語法,因為我沒有使用列表/字典。 但是,有沒有人有任何建議?
這是我用於多個僅垂直軸的內容:
f, axes = plt.subplots(nrows=3, ncols=1, figsize=(12,5))
try:
iter(axes) # Check if axes can be iterated over
except TypeError:
axes = [axes] # If not, then make it into a list
for ax in axes:
ax.plot(x, y)
ax.set_ylabel('AVG')
通常,我什至根據我有多少行來更改圖形大小。 如果您的ncols > 1
則這樣做會有些復雜。 因此,這僅適用於垂直圖。
將您的軸放在列表中:
my_axes = []
for n in range(1, 4):
my_axes.append(fig.add_subplot(1, 3, n))
for ax in my_axes:
ax.set_ylabel('AVG')
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.