Apparently you must convert the object to a NumPy array via pandas.DataFrame.to_numpy()
for plotting with matplotlib.pyplot.plot()
.
For this error:
ValueError: Multi-dimensional indexing (e.g. `obj[:, None]`) is no longer supported. Convert to a numpy array before indexing instead.
Full context:
Traceback (most recent call last):
File "/home/gabriel/my/file.py", line 120, in <module>
main()
File "/home/gabriel/my/file.py", line 98, in main
plt.plot(
File "/usr/lib/python3/dist-packages/matplotlib/pyplot.py", line 2757, in plot
return gca().plot(
File "/usr/lib/python3/dist-packages/matplotlib/axes/_axes.py", line 1632, in plot
lines = [*self._get_lines(*args, data=data, **kwargs)]
File "/usr/lib/python3/dist-packages/matplotlib/axes/_base.py", line 312, in __call__
yield from self._plot_args(this, kwargs)
File "/usr/lib/python3/dist-packages/matplotlib/axes/_base.py", line 487, in _plot_args
x = _check_1d(xy[0])
File "/usr/lib/python3/dist-packages/matplotlib/cbook/__init__.py", line 1327, in _check_1d
ndim = x[:, None].ndim
File "/home/gabriel/.local/lib/python3.10/site-packages/pandas/core/series.py", line 1072, in __getitem__
return self._get_with(key)
File "/home/gabriel/.local/lib/python3.10/site-packages/pandas/core/series.py", line 1082, in _get_with
return self._get_values_tuple(key)
File "/home/gabriel/.local/lib/python3.10/site-packages/pandas/core/series.py", line 1122, in _get_values_tuple
disallow_ndim_indexing(result)
File "/home/gabriel/.local/lib/python3.10/site-packages/pandas/core/indexers/utils.py", line 341, in disallow_ndim_indexing
raise ValueError(
ValueError: Multi-dimensional indexing (e.g. `obj[:, None]`) is no longer supported. Convert to a numpy array before indexing instead.
.values
works:
plt.plot(df['var_name'].values, df['other_var_name'].values)
...but the pandas.DataFrame.values
documentation states:
Warning: We recommend using DataFrame.to_numpy()
instead.
So, .to_numpy()
is recommended instead:
plt.plot(df['var_name'].to_numpy(), df['other_var_name'].to_numpy())
DataFrame.to_numpy()
instead. – Cardinale