2

I've read the answers related to this already and have followed this:

fig = plt.figure()
fig.add_subplot(221)   #top left
fig.add_subplot(222)   #top right
fig.add_subplot(223)   #bottom left
fig.add_subplot(224)   #bottom right 
plt.show()

Which is great, now I have a figure with four empty subplots.

What I can't figure out is how to set the #top left subplot to show my plot x where x is a matplotlib.axes.AxesSubplot that I've already generated.

My code in total is:

plt1 = plot.scatter(foo,bar)
plt2 = plot.line(bar, baz)
plt3 = plot.scatter(you, get)
plt4 = plot.line(the, picture)

fig = plt.figure()
fig.add_subplot(221)   #top left
fig.add_subplot(222)   #top right
fig.add_subplot(223)   #bottom left
fig.add_subplot(224)   #bottom right 

# Here is where I got lost
# I want figure subplot 221 to show plt1 etc...
Tyler Wood
  • 1,947
  • 2
  • 19
  • 29

2 Answers2

4

You need to keep track the Axes objects returned by add_subplot.

fig = plt.figure()
ax1 = fig.add_subplot(221)   #top left
ax2 = fig.add_subplot(222)   #top right
ax3 = fig.add_subplot(223)   #bottom left
ax4 = fig.add_subplot(224)   #bottom right 

And then you can do:

ax1.plot(...)
ax2.boxplot(...)
ax3.hist(...)
# etc

But I would just do this:

fig, (ax1, ax2, ax3, ax4) = plt.subplots(nrows=2, ncols=2)

and go from there.

Paul H
  • 65,268
  • 20
  • 159
  • 136
  • I guess what's hanging me up is that `ax1.plot(...)` strips out a lot of the formatting I've been able to achieve with `df[['col1','col2']].plot()`, namely displaying the index of datestamps as the x-axis rather than index n, and axis titles etc... The plot I like is generated by `statusPlt = df[['a','b','c']].plot(title='Status of stuff by Create Date')` which shows me a nice line chart, grouped by column value over the date index Replacing it with `ax1.plot(df[['a','b','c']])` shows the x axis as index value – Tyler Wood May 27 '16 at 00:14
  • 1
    so you should do `df[['col1','col2']].plot(ax=ax1)` – Paul H May 27 '16 at 00:14
  • 1
    @TylerWood and set the formatting on the axes directly (e.g., `ax.set_xlabel(...)`) – Paul H May 27 '16 at 00:15
  • Ahhh, this `ax` variable is what was escaping me. the fig has different `ax` created as `ax1...axn` through the `fig.add_subplot` statements and then I can use those to tell which plots to put where. – Tyler Wood May 27 '16 at 00:28
  • 1
    @TylerWood exactly -- the only time you should use the `plt` module is to explicitly create axes and figure object. after thatm you should operate on those objects directly (or specify them in e.g., pandas plotting methods). – Paul H May 27 '16 at 00:58
1

Is it principal to draw before creating subplots? If no, try this code:

fig = plt.figure()
fig.add_subplot(221)   #top left
plt1 = plt.scatter(foo,bar)
fig.add_subplot(222)   #top right
plt2 = plt.line(bar, baz)
fig.add_subplot(223)   #bottom left
plt3 = plt.scatter(you, get)
fig.add_subplot(224)   #bottom right 
plt4 = plt.line(the, picture)
Serenity
  • 35,289
  • 20
  • 120
  • 115
  • Only because I already have the code for the plots set up how I want them to appear. Going this route will require again setting axes labels, titles, legends, axes scales etc... – Tyler Wood May 26 '16 at 23:27