我想在 for 回圈中生成幾個子圖,但總是得到AttributeError: 'numpy.ndarray' object has no attribute 'plot'.
measurements = 9
fig, axs = plt.subplots(3,(measurements 2)//3, sharey=False, gridspec_kw={'wspace': 0.1},figsize =(20, 10))
for i in range(0,measurements):
try:
axs[0].plot(dfrld_1['RLD date(' str(i) ')'], dfrld_1['Depth'],lw=2, color=next(red1))
axs[1].plot(dfrld_2['RLD date(' str(i) ')'], dfrld_2['Depth'],lw=2, color=next(red1))
axs[2].plot(dfrld_3['RLD date(' str(i) ')'], dfrld_3['Depth'],lw=2, color=next(red1))
axs[3].plot(dfrld_4['RLD date(' str(i) ')'], dfrld_4['Depth'],lw=2, color=next(red1))
axs[4].plot(dfrld_5['RLD date(' str(i) ')'], dfrld_5['Depth'],lw=2, color=next(red1))
axs[5].plot(dfrld_6['RLD date(' str(i) ')'], dfrld_6['Depth'],lw=2, color=next(red1))
axs[6].plot(dfrld_7['RLD date(' str(i) ')'], dfrld_7['Depth'],lw=2, color=next(red1))
axs[7].plot(dfrld_8['RLD date(' str(i) ')'], dfrld_8['Depth'],lw=2, color=next(red1))
axs[8].plot(dfrld_9['RLD date(' str(i) ')'], dfrld_9['Depth'],lw=2, color=next(red1))
except KeyError:
pass
plt.show()
所有資料幀(dfrld_1, dfrld2,...)都具有相同的格式。
Depth RLD date(1) RLD date(2) RLD date(3) RLD date(4) RLD date(5) RLD date(6) RLD date(7) RLD date(8) RLD date(9)
0 -10 0.0 0.0 0.135132 0.697177 0.815027 0.571775 0.467736 0.152040 0.168436
1 -20 0.0 0.0 0.059295 0.286268 0.383652 0.218391 0.155417 0.065664 0.057106
2 -40 0.0 0.0 0.000000 0.052796 0.151876 0.297082 0.304435 0.268104 0.247111
3 -60 0.0 0.0 0.000000 0.000000 0.090140 0.145970 0.196542 0.148141 0.145210
4 -80 0.0 0.0 0.000000 0.000000 0.000000 0.000000 0.135144 0.124805 0.114642
我得到的確切錯誤是:
AttributeError: 'numpy.ndarray' object has no attribute 'plot'
使用另一個看起來相同且只有 3 個子圖的資料框,它可以正常作業,沒有任何問題。但在這里我找不到我的錯誤。
uj5u.com熱心網友回復:
根據docs,numpy.ndarray沒有plot方法,這是pyplot. 您呼叫subplots, 根據docs,回傳axes.Axes或軸陣列。由于可以有兩種不同的回傳型別,因此應該清楚問題的原因是什么。當您收到一個陣列作為回傳時,您將需要處理這種情況。
您可能想閱讀有關在這樣的來源繪制陣列的資訊:https : //www.codegrepper.com/code-examples/python/how to plot numpy array in matplotlib
警告:我在 Python 或 MatPlotLib 方面沒有任何經驗,我在這個答案中提供的所有資訊都是純理論的,并且基于我在搜索概念以幫助您時在檔案中找到的文章。
uj5u.com熱心網友回復:
問題是這axs是一個形狀為 (3,3) 而不是 (9, 1) 的 numpy 陣列,正如我在代碼中所想的那樣。
嘗試:
measurements = 9
fig, axs = plt.subplots(3,(measurements 2)//3, sharey=False, gridspec_kw={'wspace': 0.1},figsize =(20, 10))
for i in range(0,measurements):
try:
axs[0][0].plot(dfrld_1['RLD date(' str(i) ')'], dfrld_1['Depth'],lw=2, color=next(red1))
axs[0][1].plot(dfrld_2['RLD date(' str(i) ')'], dfrld_2['Depth'],lw=2, color=next(red1))
axs[0][2].plot(dfrld_3['RLD date(' str(i) ')'], dfrld_3['Depth'],lw=2, color=next(red1))
axs[1][0].plot(dfrld_4['RLD date(' str(i) ')'], dfrld_4['Depth'],lw=2, color=next(red1))
axs[1][1].plot(dfrld_5['RLD date(' str(i) ')'], dfrld_5['Depth'],lw=2, color=next(red1))
axs[1][2].plot(dfrld_6['RLD date(' str(i) ')'], dfrld_6['Depth'],lw=2, color=next(red1))
axs[2][0].plot(dfrld_7['RLD date(' str(i) ')'], dfrld_7['Depth'],lw=2, color=next(red1))
axs[2][1].plot(dfrld_8['RLD date(' str(i) ')'], dfrld_8['Depth'],lw=2, color=next(red1))
axs[2][2].plot(dfrld_9['RLD date(' str(i) ')'], dfrld_9['Depth'],lw=2, color=next(red1))
except KeyError:
pass
plt.show()
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/316920.html
上一篇:foldTree的分步評估
