0

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.

2
  • 1
    Do you have a size column in your dataframe? Commented Mar 21, 2022 at 18:20
  • Yes, I have a column size in this dataframe. It contains information about the number of rows of other dataframes on which my models are trained. Commented Mar 22, 2022 at 14:39

1 Answer 1

1

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')
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.