Сохраните pandas.DataFrame.hist с несколькими осями на одной фигуре - PullRequest
0 голосов
/ 29 марта 2019

У меня есть pandas dataframe с 24 столбцами, и я использую функцию pandas.DataFrame.hist для генерации фигуры с некоторыми вспомогательными участками.

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)

Проблема заключается в том, что я пытаюсь сохранить эту фигуру в одномPNG-файл Я получаю сообщение об ошибке

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

AttributeError: у объекта 'numpy.ndarray' нет атрибута 'savefig'

Ни одна из статей, которые я проверял, поэтомуFar предлагает решение

Руководство по визуализации Pandas StackOverflow

1 Ответ

1 голос
/ 29 марта 2019

savefig не является методом объекта plot, возвращаемого df.hist. Попробуйте следующее

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'))
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...