![](/img/trans.png)
[英]IndexError: too many indices for array in python (still error occur)
[英]python error IndexError: too many indices for array
實際上,我想在 Python 中繪制多線子圖。
i,j=0
countries = ["china-2","South Korea-2"]
fig, axs = plt.subplots(2,(len(countries)+1)//2)
for country in countries:
axs[i, j].plot(total_miu,linewidth=2,color='green')
axs[i, j].plot(AR,linewidth=2,color='red')
axs[i, j].plot(PR, linewidth=2,color='blue')
axs[i, j].plot(IR*0.08, linewidth=2,color='purple')
axs[i, j].set_title(country)
if i == 0:
i = 1
else:
i = 0
j = j + 1
但它給出了錯誤 axs[i, j].plot(total_miu,linewidth=2,color='green')
IndexError: 數組索引太多我找不到原因。 你能幫我解決錯誤嗎?
我建議您查閱matplotlib.pyplot.subplots 的文檔。
subplots
有一個參數squeeze
:
matplotlib.pyplot.subplots(nrows=1, ncols=1, *, sharex=False, sharey=False, squeeze=True, subplot_kw=None, gridspec_kw=None, **fig_kw)
擠壓:布爾,默認值:真
- 如果為 True,則從返回的 Axes 數組中擠出額外的維度:
- 如果僅構建一個子圖 (nrows=ncols=1),則生成的單個軸 object 作為標量返回。
- 對於 Nx1 或 1xM 子圖,返回的 object 是 Axes 對象的一維 numpy object 數組。
- 對於 NxM,N>1 和 M>1 的子圖作為二維數組返回。
- 如果為 False,則根本不進行壓縮:返回的 Axes object 始終是一個包含 Axes 實例的二維數組,即使它最終是 1x1。
countries
的長度為 2,這使得ncols=1
,並且2 x 1
子圖被壓縮為一維 object。
如果要將axes
區設為 2D 數組,請將squeeze
設置為False
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.