I have historic data on my stock holdings in a database. The object here is to create two charts and save each to png files. I read the data from the db into a pandas dataframe to build the charts. The code runs cleanly - no errors. I see the charts when I run it in Spyder, but nothing is saved. Here is a brief summary of what I'm doing:
fig1, ax1 = plt.subplots()
... some code
df['Price'].plot(ax=ax1, title=title1)
plt.savefig(figName1, bbox_inches='tight')
fig2, ax2 = plt.subplots()
... some code
df['% Price'].plot(ax=ax2, title=title2)
df['% S&P'].plot(ax=ax2)
plt.savefig(figName2, bbox_inches='tight')
Is there something wrong with this approach? Thanks.
CodePudding user response:
I think the problem is that when you call DataFrame.plot()
it returns a numpy array of matplotlib.axes.Axes objects. Try to use get_figure()
like this:
fig1 = df['Price'].plot(ax=ax1, title=title1).get_figure()
fig1.savefig(figName1, bbox_inches='tight')
Or in one line:
df['Price'].plot(ax=ax1, title=title1).get_figure().savefig(figName1, bbox_inches='tight')
CodePudding user response:
Fixed. There was nothing wrong with the code shown. The problem was with the format of the figName1 and figName2 addresses. Embarrassing.