0

I have a pandas dataframe with 24 columns, and I use the function pandas.DataFrame.hist to generate a figure with some subplots.

plot = df.hist(figsize = (20, 15))
plot


array([[<matplotlib.axes._subplots.AxesSubplot object at 0x0000000018D47EB8>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x000000001C1200B8>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x000000001C1EADD8>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x000000001C20A4A8>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x000000001B61AB38>],
       [<matplotlib.axes._subplots.AxesSubplot object at 0x000000001B61AB70>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x000000001B671898>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x000000001B698F28>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x000000001B6C85F8>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x000000001B6F2C88>],
       [<matplotlib.axes._subplots.AxesSubplot object at 0x000000001B723358>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x000000001B74A9E8>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x000000001B77B0B8>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x000000001B7A1748>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x000000001B7C8DD8>],
       [<matplotlib.axes._subplots.AxesSubplot object at 0x000000001B7F84A8>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x000000001B821B38>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x000000001B853208>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x000000001B87B898>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x000000001B8A2F28>],
       [<matplotlib.axes._subplots.AxesSubplot object at 0x000000001B8D35F8>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x000000001B8FAC88>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x000000001B92C358>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x000000001B9549E8>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x000000001B9850B8>]],
      dtype=object)

The problem is when I try to save this figure in a single PNG file I get an error

plot.savefig(os.path.join(folder_wd, folder_output, folder_dataset,'histogram.png'))

AttributeError: 'numpy.ndarray' object has no attribute 'savefig'

None of the articles that I have checked so far has offered a solution

Pandas visualisation guide StackOverflow

Jespar
  • 1,017
  • 5
  • 16
  • 29

1 Answers1

1

savefig is not a method of the plot object returned by the df.hist. Try the following

import matplotlib.pyplot as plt

# rest of your code

plot = df.hist(figsize = (20, 15))
plt.savefig(os.path.join(folder_wd, folder_output, folder_dataset,'histogram.png'))
Sheldore
  • 37,862
  • 7
  • 57
  • 71