get_axes not present?

Mahmood Naderan nt_mahmood at yahoo.com
Sun Nov 21 14:21:52 EST 2021


>Your example isn't minimal enough for me to be able to pin it down any
>better than that, though.

Chris,
I was able to simply it even further. Please look at this:


$ cat test.batch.csv
Value,Value
10,2
5,2
10,2


$ cat test.py
import pandas as pd
import csv,sys
import matplotlib
import matplotlib.pyplot as plt

df = pd.read_csv('test.batch.csv')
print(df)

def plot_dataframe(df, cnt, axes):
    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("cnt=", cnt)
    print("row=", row)
    ax1 = row.plot(label=cnt, ax=axes[0], marker='o')   # Line chart
    ax1.set_ylabel( 'test', fontsize=15 )
    plt.subplot(2, 1, 2)
    df2 = row.value_counts()
    df2.reindex().plot(kind='bar', label=cnt, ax=axes[1])   # Histogram

def plot_kernels(df):
    fig,axes = plt.subplots(2,1, figsize=(20, 15))
    cnt=1
    plot_dataframe(df, cnt, axes)
    cnt = cnt + 1
    for ax in axes:
        ax.legend()
    plt.show()

print("matplotlib version = ",  matplotlib.__version__)
print("pandas version = ", pd.__version__)
print("sys version", sys.version_info)

plot_kernels(df)




And the output is


$ 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)
cnt= 1
row= 1    10
2     2
Name: 0, dtype: int64
Traceback (most recent call last):
  File "test.py", line 41, in <module>
    plot_kernels(df)
  File "test.py", line 29, in plot_kernels
    plot_dataframe(df, cnt, axes)
  File "test.py", line 19, in plot_dataframe
    ax1 = row.plot(label=cnt, ax=axes[0], marker='o')   # Line chart
  File "/home/mnaderan/.local/lib/python3.8/site-packages/pandas/plotting/_core.py", line 955, in __call__
    return plot_backend.plot(data, kind=kind, **kwargs)
  File "/home/mnaderan/.local/lib/python3.8/site-packages/pandas/plotting/_matplotlib/__init__.py", line 61, in plot
    plot_obj.generate()
  File "/home/mnaderan/.local/lib/python3.8/site-packages/pandas/plotting/_matplotlib/core.py", line 283, in generate
    self._adorn_subplots()
  File "/home/mnaderan/.local/lib/python3.8/site-packages/pandas/plotting/_matplotlib/core.py", line 483, in _adorn_subplots
    all_axes = self._get_subplots()
  File "/home/mnaderan/.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'



Any idea about that?



Regards,
Mahmood



More information about the Python-list mailing list