2

i am quite new to pandas plot facilities, in the doc, the following command is really handy:

myplot = rts.ret.hist(bins=50, by=rts.primary_mic)

however, the problems comes when i try to get a figure reference from the plot and save it:

myfigure = myplot.get_figure()
AttributeError: 'numpy.ndarray' object has no attribute 'get_figure'

what I understand is, rts.ret.hist(bins=50) returns a plot object, while rts.ret.hist(bins=50 returns an array object.

how should I save my figure in this case?

any clue?

thanks!

James Bond
  • 7,533
  • 19
  • 50
  • 64

1 Answers1

4

To save the figure, you could use plt.savefig:

import pandas as pd
import matplotlib.pyplot as plt

df = pd.DataFrame(
    [(1, 2), (1, 3), (1, 4), (2, 1), (2, 2)], columns=['col1', 'col2'])
df.hist(bins=4, by=df['col1'])
plt.savefig('/tmp/out.png')

enter image description here

unutbu
  • 842,883
  • 184
  • 1,785
  • 1,677
  • trying to use this with pandas.plotting.scatter_matrix(). It only saves a blank image. – Selah Aug 10 '17 at 20:11
  • @Selah: Be sure you call `plt.savefig` *before* calling `plt.show()` -- that might be the cause of the problem, if you are calling `plt.show()`. If that doesn't solve the problem, please open a new question with a short runnable example so we can understand and reproduce the problem. – unutbu Aug 10 '17 at 20:32
  • 1
    No luck. I think my issue might be that the scatter plot saves as an array, and plot.show() doesn't work on that. Thanks for the input though. – Selah Aug 11 '17 at 18:19