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.
I found this as a related question.
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()
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')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With