Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

save dataframe.hist() to a file [duplicate]

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!

like image 953
user36869 Avatar asked Jul 23 '15 19:07

user36869


1 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')
like image 192
Jianxun Li Avatar answered Sep 20 '22 03:09

Jianxun Li