Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ValueError: Multi-dimensional indexing (e.g. `obj[:, None]`) is no longer supported. Convert to a numpy array before indexing instead

I am trying to plot a histogram using seaborn. When I try to set kde=True this error is returned: ValueError: Multi-dimensional indexing (e.g. obj[:, None]) is no longer supported. Convert to a numpy array before indexing instead.

sns.histplot(data=df, x='age', kde=True);

How can I solve this?

like image 547
Flavio Brienza Avatar asked Sep 08 '25 12:09

Flavio Brienza


2 Answers

I believe that you have an incompatibility between your version of matplotlib and your version of pandas, with seaborn caught in the middle (source: https://github.com/mwaskom/seaborn/issues/3312)

like image 192
mwaskom Avatar answered Sep 10 '25 06:09

mwaskom


This can also happen in Matplotlib. I have a virtual environment with matplotlib=3.3.0 and pandas=2.0.2.

A workaround is to use the dataframe's values attribute which will return a numpy array, which can then be used in the plotting function:

plt.plot(df['var_name'].values, df['other_var_name'].values)
like image 25
cwa Avatar answered Sep 10 '25 06:09

cwa