I am using this code which provides nice plots one after the next (using IPython-notebook & Pandas)
for subsm in subsl:
H7, subsm = sumsubdesc2(table, subsm)
ax1=H7.plot()
plt.title('Rolling 4q mean %s'%(subsm))
ax1.set_title('Rolling 4q mean %s'%(subsm))
ax1.set_ylim(100000,600000)
I'd like to get the plots "2up" one next to the next for 3 rows total (5 subplots) can't figure out how to handle that since all the subplot examples seem to be for subplotting ether the data or specific plots and specific grid placement.
So I don't know how to create the main plot and then subplot a number of graphs (in this case 5) with titles as two-up?
Edit line two of code since I left out the function call ;-(
-
Do you know the number of subplots before-hand? Or should your plot grow dynamically?Schorsch– Schorsch2013年09月19日 17:55:22 +00:00Commented Sep 19, 2013 at 17:55
-
I'd like a dynamic solution although in the specific instance I do know that I have 5 chartsdartdog– dartdog2013年09月19日 18:19:43 +00:00Commented Sep 19, 2013 at 18:19
2 Answers 2
Here's what you need to do:
import math
import matplotlib.pylab as plt
nrows = int(math.ceil(len(subsl) / 2.))
fig, axs = plt.subplots(nrows, 2)
ylim = 100000, 600000
for ax, subsm in zip(axs.flat, subsl):
H7, subsm = sumsubdesc2(table, subsm)
H7.plot(ax=ax, title='Rolling 4q mean %s' % subsm)
ax.set_ylim(ylim)
This will work even if axs.size > len(subsl)
since StopIteration
is raised when the shortest iterable runs out. Note that axs.flat
is an iterator over the row-order flattened axs
array.
To hide the last plot that isn't showing, do this:
axs.flat[-1].set_visible(False)
More generally, for axs.size - len(subsl)
extra plots at the end of the grid do:
for ax in axs.flat[axs.size - 1:len(subsl) - 1:-1]:
ax.set_visible(False)
That slice looks a little gnarly, so I'll explain:
The array axs
has axs.size
elements. The index of the last element of the flattened version of axs
is axs.size - 1
. subsl
has len(subsl)
elements and the same reasoning applies about the index of the last element. But, we need to move back from the last element of axs
to the last plotted element so we need to step by -1.
-
@dartdog I don't understand your comment.Phillip Cloud– Phillip Cloud2013年09月19日 22:21:59 +00:00Commented Sep 19, 2013 at 22:21
-
sorry got an error on 3 fig, axs = plt.subplots(nrows, 2, sharey=True)>>>range() integer end argument expected, got float.dartdog– dartdog2013年09月19日 22:28:12 +00:00Commented Sep 19, 2013 at 22:28
-
The answer below is giving me a decent result but it has re-labeled the x axis from 0-70 instead of using the dates as it did when plotted directly?dartdog– dartdog2013年09月19日 22:31:14 +00:00Commented Sep 19, 2013 at 22:31
-
Just needed to coerce the result of
math.ceil
toint
(only in Python 2, FYI)Phillip Cloud– Phillip Cloud2013年09月19日 22:34:50 +00:00Commented Sep 19, 2013 at 22:34 -
That did it, I even have the years on the x axis I do have one blank chart at the end Any way to delete it?dartdog– dartdog2013年09月19日 22:37:49 +00:00Commented Sep 19, 2013 at 22:37
I'm not sure, but I think what you're asking is
# not tested
import math
import matplotlib.pylab as plt
Nrows = math.ceil(len(subsl) / 2.)
for i in range(len(subsl)):
subsm = subsl[i]
H7, subsm = sumsubdesc2(table, subsm)
plt.subplot(Nrows, 2, i+1)
# do some plotting
plt.title('Rolling 4q mean %s'%(subsm))
I'm not sure what you mean by "titles as two-up."
-
Looks good let me give it a go. but I want to associate the title with each chart instance using the variable ('Rolling 4q mean %s'%(subsm))dartdog– dartdog2013年09月19日 18:17:05 +00:00Commented Sep 19, 2013 at 18:17
-
My edit with the function call above has screwed up my question Still stuck on how to loop this..dartdog– dartdog2013年09月19日 18:30:17 +00:00Commented Sep 19, 2013 at 18:30
-
@dartdog. I edited my response to do what I think you're asking (and fix a bug).Greg Whittier– Greg Whittier2013年09月19日 18:32:09 +00:00Commented Sep 19, 2013 at 18:32
-
Don't see how to incorporate: H7, subsm = sumsubdesc2(table, subsm) ax1=H7.plot()dartdog– dartdog2013年09月19日 19:02:03 +00:00Commented Sep 19, 2013 at 19:02
-
@dartdog I modified it so that subsm is calculated with your sumsubdesc2 function for each loop (this wasn't in your original code as I recall). I'm not exactly sure what the function does, so this is my best guess on what will work.Greg Whittier– Greg Whittier2013年09月19日 19:52:46 +00:00Commented Sep 19, 2013 at 19:52
Explore related questions
See similar questions with these tags.