6

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!

Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
user36869
  • 63
  • 1
  • 3

1 Answers1

13

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')
Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
Jianxun Li
  • 24,004
  • 10
  • 58
  • 76
  • 1
    That worked fine, thank you! Can you explain why, though? It almost seems superfluous to have to use plt.subplots() every time I want to save a single diagram. I'm also still curious to know why get_figure() didn't work in my context. – user36869 Jul 23 '15 at 19:14
  • @user36869 `fig, ax` are created in pair when you call `plt.subplots()`. Alternatively, just based on the specific error you saw, the `ax` object is inside the `numpy.array`, you can use `fig=ax[0][0].get_figure()` to return the `figure` object as well. That two dimension is reserved to `nrows` and `ncols` when you put multiple subfigure in a single big figure. – Jianxun Li Jul 23 '15 at 19:21
  • If no $DISPLAY is available, you need to prepend `import matplotlib` and `matplotlib.use('Agg')` (has to appear before pyplot import and possibly pandas import) – Enno Gröper Nov 03 '15 at 11:50