如何将 pandas .plot() 传输到 Matplotlib .errorbar()
我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
pandas.DataFrame
对象上有一个名为size
的属性,它是一个数字,等于 DataFrame 中的单元格数量(中值的乘积) df.shape )。您正在尝试访问名为
size
的列,但 pandas 在选择列名称大小
。由于单个数字的形状为 1,但数据帧中的列的长度为 12,因此形状不匹配。相反,使用字符串来索引数据帧并获取列:
There is a property on
pandas.DataFrame
objects namedsize
, and it's a number, equal to the number of cells in the DataFrame (the product of the values indf.shape
). You're trying to access a column namedsize
, but pandas chooses the property namedsize
before it chooses the column namesize
. 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: