简体   繁体   English

如何将 pandas.plot() 转换为 Matplotlib.errorbar()

[英]How to transfer pandas .plot() to Matplotlib .errorbar()

I'm looking to plot error bars on a line plot I did using pandas's .plot() function我正在寻找 plot 行上的错误栏plot 我确实使用了 pandas 的.plot() function

scores_xgb.plot(x='size', y='MSE_mean_tot', kind='line',logx=True,title='XGBoost 5 samples, 5 fold CV')

Running this gives me the following plot:运行它会得到以下 plot:

For plotting the error bars, I choose to use .errorbar() from Matplotlib. When running the cell为了绘制错误栏,我选择使用.errorbar() 。运行单元格时

plt.errorbar(x=scores_xgb.size, y=scores_xgb.MSE_mean_tot, yerr=std_xgb ,title='XGBoost 5 samples, 5 fold CV')
plt.show()

I receive the following error message:我收到以下错误消息:

ValueError: 'x' and 'y' must have the same size

This confuses me, as I use the same Dataframe in both examples, each time using the same variable for x and y respectively, therefore it has the same size (12) both times.这让我感到困惑,因为我在两个示例中都使用了相同的 Dataframe,每次分别对xy使用相同的变量,因此它两次都具有相同的大小 (12)。

NB: the yerr = std_xgb also has size 12.注意: yerr = std_xgb的大小也为 12。

There is a property on pandas.DataFrame objects named size , and it's a number, equal to the number of cells in the DataFrame (the product of the values in df.shape ). pandas.DataFrame对象上有一个名为size的属性,它是一个数字,等于 DataFrame 中的单元格数( df.shape中值的乘积)。 You're trying to access a column named size , but pandas chooses the property named size before it chooses the column name size .您正在尝试访问名为size,但 pandas 在选择列名称size之前选择了名为size的属性。 Since the single number has a shape of 1 but the columns in the dataframe have a length of 12, you're getting a shape mismatch.由于单个数字的形状为 1,但 dataframe 中的列的长度为 12,因此形状不匹配。

Instead, use strings to index the dataframe and get the the columns:相反,使用字符串索引 dataframe 并获取列:

plt.errorbar(x=scores_xgb['size'], y=scores_xgb['MSE_mean_tot'], yerr=std_xgb, title='XGBoost 5 samples, 5 fold CV')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM