About get_axes() in Pandas 1.2.3
Mahmood Naderan
nt_mahmood at yahoo.com
Mon Nov 22 04:03:53 EST 2021
Hi
I asked a question some days ago, but due to the lack of minimal producing code, the topic got a bit messy. So, I have decided to ask it in a new topic with a clear minimum code.
With Pandas 1.2.3 and Matplotlib 3.3.4, the following plot() functions returns error and I don't know what is wrong with that.
import pandas as pd
import csv,sys
import matplotlib
import matplotlib.pyplot as plt
df = pd.read_csv('test.batch.csv')
print(df)
print("matplotlib version = ", matplotlib.__version__)
print("pandas version = ", pd.__version__)
print("sys version", sys.version_info)
fig,axes = plt.subplots(2,1, figsize=(20, 15))
df.columns = range(1, len(df.columns)+1) # Ignore the column header
row = df.iloc[0].astype(int) # First row in the dataframe
plt.subplot(2, 1, 1)
print("axes=", axes)
print("axes[0]=", axes[0])
print("row=", row)
ax1 = row.plot(ax=axes[0]) # Line chart <-- ERROR
ax1.set_ylabel( 'test' )
plt.subplot(2, 1, 2)
df2 = row.value_counts()
df2.reindex().plot(kind='bar', ax=axes[1]) # Histogram
plt.show()
The output is
$ cat test.batch.csv
Value,Value
10,2
5,2
10,2
$ python3 test.py
Value Value.1
0 10 2
1 5 2
2 10 2
matplotlib version = 3.3.4
pandas version = 1.2.3
sys version sys.version_info(major=3, minor=8, micro=10, releaselevel='final', serial=0)
axes= [<AxesSubplot:> <AxesSubplot:>]
axes[0]= AxesSubplot(0.125,0.53;0.775x0.35)
row= 1 10
2 2
Name: 0, dtype: int64
Traceback (most recent call last):
File "test.py", line 20, in <module>
ax1 = row.plot(ax=axes[0]) # Line chart
File "/home/mahmood/.local/lib/python3.8/site-packages/pandas/plotting/_core.py", line 955, in __call__
return plot_backend.plot(data, kind=kind, **kwargs)
File "/home/mahmood/.local/lib/python3.8/site-packages/pandas/plotting/_matplotlib/__init__.py", line 61, in plot
plot_obj.generate()
File "/home/mahmood/.local/lib/python3.8/site-packages/pandas/plotting/_matplotlib/core.py", line 283, in generate
self._adorn_subplots()
File "/home/mahmood/.local/lib/python3.8/site-packages/pandas/plotting/_matplotlib/core.py", line 483, in _adorn_subplots
all_axes = self._get_subplots()
File "/home/mahmood/.local/lib/python3.8/site-packages/pandas/plotting/_matplotlib/core.py", line 903, in _get_subplots
ax for ax in self.axes[0].get_figure().get_axes() if isinstance(ax, Subplot)
AttributeError: 'NoneType' object has no attribute 'get_axes'
Although the plot() crashes, I see that row and axes variables are valid. So, I wonder what is the workaround for this code without upgrading Pandas or Matplotlib. Any idea?
Regards,
Mahmood
More information about the Python-list
mailing list