3

I'm plotting some data based on pandas dataframes and series. Following is a part of my code. This code gives an error.

RuntimeError: underlying C/C++ object has been deleted


from matplotlib import pyplot as plt
from matplotlib.backends.backend_pdf import PdfPages
fig = plt.figure()

dfs = df['col2'].resample('10t', how='count')
dfs.plot()
plt.show()

reg = df.groupby('col1').size()
reg.sort()
reg[-10:].plot(kind='barh')
plt.show()

pp = PdfPages('foo.pdf')
fig.savefig(pp, format='pdf') 
pp.close()

I have two questions.

  1. How to plot multiple plots in one output?(Here I get multiple outputs for each and every plot)
  2. How to write all these plots in to one pdf?

I found this as a related question.

7
  • As i know subplot is the answer to my first question? How to use it? Commented Jul 8, 2013 at 9:27
  • Are you looking for a generic answer? Because we have no idea what your dataset looks like! Commented Jul 8, 2013 at 9:30
  • @Sukrit Kalra : Yes, a generic answer? Explanation is enough? Commented Jul 8, 2013 at 9:33
  • Hi! Were you looking for this kind of a plot? See my answer. Commented Jul 8, 2013 at 10:01
  • Remember to accept your own answer when it will let you, and can you make it clear what error you were getting? Commented Jul 8, 2013 at 12:46

3 Answers 3

4

Following is the part of code which gave me the expected result, there may be more elegant ways to do this;

def plotGraph(X):
    fig = plt.figure()
    X.plot()
    return fig


plot1 = plotGraph(dfs)
plot2 = plotGraph2(reg[:-10])
pp = PdfPages('foo.pdf')
pp.savefig(plot1)
pp.savefig(plot2)
pp.close()
Sign up to request clarification or add additional context in comments.

Comments

2

Please see the following for targeting different subplots with Pandas.

I am assuming you need 2 subplots (in row fashion). Thus, your code may be modified as follows:

from matplotlib import pyplot as plt

fig, axes = plt.subplots(nrows=2)

dfs = df['col2'].resample('10t', how='count')
dfs.plot(ax=axes[0])

reg = df.groupby('col1').size()
reg.sort()
reg[-10:].plot(kind='barh',ax=axes[0])

plt.savefig('foo.pdf')

3 Comments

Thanks Batra! Any way I found a different approach to do this
You may wish to then add your own answer for the same, so that others might benefit by the same.
Ok, sure :) I'll provide it!
1

matplotlib merges the plots to one figure by default. See the following snippet -

>>> import pylab as plt
>>> randomList = [randint(0, 40) for _ in range(10)]
>>> randomListTwo = [randint(0, 40) for _ in range(10)]
>>> testFigure = plt.figure(1)
>>> plt.plot(randomList)
[<matplotlib.lines.Line2D object at 0x03F24A90>]
>>> plt.plot(randomListTwo)
[<matplotlib.lines.Line2D object at 0x030B9FB0>]
>>> plt.show()

Gives you a figure like the following -

enter image description here

Also, the file can be easily saved in PDF through the commands you posted -

>>> from matplotlib.backends.backend_pdf import PdfPages
>>> pp = PdfPages('foo.pdf')
>>> testFigure.savefig(pp, format='pdf')
>>> pp.close()

This gave me a PDF with a similar figure.

1 Comment

Hey,Thanks very much! Anyway by the time I figured out how to do it :)!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.