I am attempting to create a dataframe histogram and save it as a file.
Here is my code:
ax=df.hist('ColumnName')
fig=ax.get_figure()
fig.savefig('pictureName.png', dpi=100, bbox_inches='tight')
The first line works fine; however, the second line returns an error: AttributeError: 'numpy.ndarray' object has no attribute 'get_figure'.
Because this question shows the get_figure() being applied to series.hist(), I have also tried using ax=df['ColumnName'].hist()
, which successfully produced a histogram but led to the same error message when I attempted to implement get_figure().
As recommended in this other question, normally I would skip the get_figure() and the fig.savefig(), opting instead for plt.savefig, but I am making multiple figures. In my experience, plt.savefig() is unreliable in saving multiple figures, instead saving one figure multiple times, even when I use fig.close() after each figure creation and save.
I very much want to solve this problem as neatly as possible, so that I can carry the solution smoothly into other applications, rather than having to use a different duct-tape fix every time I have to make a graph.
Thank you for your help!
Can you try the following code?
import pandas as pd
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
df.hist('ColumnName', ax=ax)
fig.savefig('example.png')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With