如何将 pandas .p​​lot() 传输到 Matplotlib .errorbar()

发布于 2025-01-15 20:05:02 字数 788 浏览 0 评论 0原文

我正在使用 pandas 的 .plot() 函数在我绘制的线图上绘制误差线

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

运行此函数会得到以下图:

为了绘制误差线,我选择使用 Matplotlib 中的 .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()

我收到以下错误消息:

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

这让我感到困惑,因为我在两个示例中使用相同的 Dataframe,每次都对 x使用相同的变量 >y 分别,因此它两次具有相同的大小 (12)。

注意: yerr = std_xgb 的尺寸也为 12。

I'm looking to plot error bars on a line plot I did using pandas's .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:

For plotting the error bars, I choose to use .errorbar() from Matplotlib. When running the cell

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.

NB: the yerr = std_xgb also has size 12.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

初见 2025-01-22 20:05:03

pandas.DataFrame 对象上有一个名为 size 的属性,它是一个数字,等于 DataFrame 中的单元格数量(中值的乘积) df.shape )。您正在尝试访问名为 size,但 pandas 在选择列名称大小。由于单个数字的形状为 1,但数据帧中的列的长度为 12,因此形状不匹配。

相反,使用字符串来索引数据帧并获取列:

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

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). You're trying to access a column named size, but pandas chooses the property named size before it chooses the column name 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.

Instead, use strings to index the dataframe and get the the columns:

plt.errorbar(x=scores_xgb['size'], y=scores_xgb['MSE_mean_tot'], yerr=std_xgb, title='XGBoost 5 samples, 5 fold CV')
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文