我正在尋找在我使用熊貓函式所做的折線圖上繪制誤差線.plot()
scores_xgb.plot(x='size', y='MSE_mean_tot', kind='line',logx=True,title='XGBoost 5 samples, 5 fold CV')
運行它給了我以下情節:

為了繪制誤差線,我選擇使用.errorbar()Matplotlib。運行單元時
plt.errorbar(x=scores_xgb.size, y=scores_xgb.MSE_mean_tot, yerr=std_xgb ,title='XGBoost 5 samples, 5 fold CV')
plt.show()
我收到以下錯誤訊息:
ValueError: 'x' and 'y' must have the same size
這讓我感到困惑,因為我在兩個示例中都使用了相同的 Dataframe,每次都分別使用相同的變數x和y,因此兩次都具有相同的大小(12)。
注意:也yerr = std_xgb有 12 號。
uj5u.com熱心網友回復:
pandas.DataFrame物件上有一個名為的屬性size,它是一個數字,等于 DataFrame 中的單元格數( 中值的乘積df.shape)。您正在嘗試訪問名為的列size,但 pandas在選擇列 namesize 之前size選擇了 name 屬性。由于單個數字的形狀為 1,但資料框中的列的長度為 12,因此您會遇到形狀不匹配的問題。
相反,使用字串來索引資料框并獲取列:
plt.errorbar(x=scores_xgb['size'], y=scores_xgb['MSE_mean_tot'], yerr=std_xgb, title='XGBoost 5 samples, 5 fold CV')
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/447236.html
標籤:Python 熊猫 matplotlib 误差线
上一篇:如何在圖表中添加列標簽
