user1911092
user1911092

Reputation: 4241

Subplots not showing up in plot - pyplots

I recently upgraded my pandas python and my pyplots program that used to work won't process subplots the same way. The script before had three charts on the one figure stacked on top of each other. Now when it runs, only the third chart shows up (in the bottom 1/3 of the screen). So I am close to debugging but am missing a step. Can someone please advise? Thank you.

f is a df:

Date    Open    High    Low Close   Volume  Adj_Close

2011-01-03  15.82   16.200001   15.78   15.8    41760200    15.014473
2011-01-04  16.450001   16.59   16.209999   16.52   75576400    15.698677
2011-01-05  16.34   16.59   16.110001   16.559999   48278700    15.736688
2011-01-06  16.719999   16.719999   16.23   16.360001   37700800    15.546632
2011-01-07  16.450001   16.469999   16.1    16.42   36302500    15.603649

Code:

ticker = 'XYZ'
plt.subplot(3, 1, 1)
f.plot(y= ['Adj_Close','Open'], title= ticker + " Close & Open")
plt.subplot(3, 1, 2)
f.plot(y= ['High', 'Low'], title='High and Low')
ax = plt.subplot(3, 1, 3)
ax.bar(f.index, f['Volume'], width = 1)
ax.set_title("Volume")
plt.gcf().set_size_inches(18.5, 18.5)
plt.tight_layout()
date_today = datetime.date.today().isoformat()
filename = '/data/file/' + ticker + "_" + date_today + ".png"
plt.gcf().savefig(filename)
plt.close()

Thank you again

Upvotes: 1

Views: 4069

Answers (1)

iayork
iayork

Reputation: 6699

I believe it's because you're not assigning axes to your first two plots. Try something like this:

fig, (ax1,ax2,ax3) = plt.subplots(3,1)
f.plot(y= ['Adj_Close','Open'], title= ticker + " Close & Open", ax=ax1)
f.plot(y= ['High', 'Low'], title='High and Low', ax=ax2)
ax3.bar(f.index, f['Volume'], width = 1)
plt.tight_layout()

so that each plot is explicitly assigned to a particular axis object.

Upvotes: 1

Related Questions