我有一个24列的pandas数据框,并且我使用函数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”
到目前为止,我没有看过任何文章提供解决方案
答案 0 :(得分:1)
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'))