save dataframe.hist() to a file

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!

Answers


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')

Need Your Help

jQuery table count rows always returns value of 1

jquery count html-table rows

For some reason every time I try to count the number of rows in a table

keylistener and runnable not working

java keylistener runnable

can you see the problem. the thread runs fine but the brick does not want to respond to the key listener. I try to test if the keylistener was at last getting an event but it does not even do the s...