 # Controlling spacing between subplots

Hi, I am trying to do a simple 2x2 box of plots. My code is so far very simple, and simply looks something like this.

ax=subplot(221)

plot(x2cp,y2cp)
ax.set_title(‘2cP’)
ylabel(‘Displacement’)
ax=subplot(222)
plot(x2cb,y2cb)
ax.set_title(‘2cB’)
ax=subplot(223)
plot(x6fp,y6fp)

ax.set_title(‘6fP’)
ax=subplot(224)
plot(x6fb,y6fb)
ax.set_title(‘6fB’)
show()

But when I do this, the text and numbers from various plots overlap each other and get in the way (I noticed this is a common problem among the images in the matplotlib gallery as well). I have 2 questions:

1). How can I control the spacing, or padding, between the plots so that the numbers don’t overlap?

2). Is there a way for me to have a single common y-axis label and x-axis label that runs along the full left-hand side and bottom, respectively?

Thanks so much for the help!

Hello again,

I’ve actually made some more progress on my last question after finding this site: http://www.scipy.org/Cookbook/Matplotlib/Multiple_Subplots_with_One_Axis_Label

and I was able to adjust the widths of my spacing.

My current plot looks like this:

http://imageshack.us/photo/my-images/849/current.png/

with source code that looks like:

adjustprops = dict(left=0.1, bottom=0.1, right=0.97, top=0.93, wspace=0.25, hspace=0.25)
fig=figure()

ax=subplot(221)
plot(x2cp,y2cp,‘bo’,markersize=3)
#ax.set_title(‘2cP’)

text(12,0.03,‘2cP’)
#ylabel(‘Displacement’)
axis([0,16,0,0.04])
bx=subplot(223)
plot(x2cb,y2cb,‘bo’,markersize=3)
#ax.set_title(‘2cB’)
text(12,0.225,‘2cB’)
axis([0,16,0,0.3])

cx=subplot(222)
plot(x6fp,y6fp,‘bo’,markersize=3)
#ax.set_title(‘6fP’)
text(12,0.03375,‘6fP’)
axis([0,16,0,0.045])
dx=subplot(224)
plot(x6fb,y6fb,‘bo’,markersize=3)
#ax.set_title(‘6fB’)

text(12,0.225,‘6fB’)
axis([0,16,0,0.3])

show()

Now the only thing I’d like to do now is create a global y-axis and a global x-axis along the bottom for all 4 plots (which the website I linked to above gives some hints about, but trying what I thought was equivalent didn’t work). And lastly, I wanted those y-axes which go from 0.000 to 0.040 to instead go from 0.00 to 0.04 (i.e., only two decimal places, getting rid of the ones that are in between).

Thanks for any guidance you can give.

Best,

···

On Mon, Sep 19, 2011 at 3:32 PM, Brad Malone <brad.malone@…287…> wrote:

Hi, I am trying to do a simple 2x2 box of plots. My code is so far very simple, and simply looks something like this.

ax=subplot(221)

plot(x2cp,y2cp)
ax.set_title(‘2cP’)
ylabel(‘Displacement’)
ax=subplot(222)
plot(x2cb,y2cb)
ax.set_title(‘2cB’)
ax=subplot(223)
plot(x6fp,y6fp)

ax.set_title(‘6fP’)
ax=subplot(224)
plot(x6fb,y6fb)
ax.set_title(‘6fB’)
show()

But when I do this, the text and numbers from various plots overlap each other and get in the way (I noticed this is a common problem among the images in the matplotlib gallery as well). I have 2 questions:

1). How can I control the spacing, or padding, between the plots so that the numbers don’t overlap?

2). Is there a way for me to have a single common y-axis label and x-axis label that runs along the full left-hand side and bottom, respectively?

Thanks so much for the help!

Not sure what you mean global axis but I think I was trying to do
something similar with this. This is the chunk of one subplot.
Specifically look at last three lines:
ax.set_title(‘b) 5’)
ax.set_ylim((0,yUpper))
for i in tempRun:
ax.plot(x,actSum[1,semi,i,semi], label=tempLabel[i],
linestyle=dashs[i%len(dashs)], color=plotColor[i%len(plotColor)])
clear_spines(ax)
ax.set_xticklabels(’’)
ax.yaxis.set_ticks_position(‘left’)
ax.xaxis.set_ticks_position(‘bottom’)

``````Cheers,
Jeff
``````
···

Hi Jeffrey,

Thanks the response. Sorry about the term “global axis”. That was clearly not the best way to say it. What I meant to say is global x axis LABEL and y-axis LABEL. This can be seen in this example (http://www.scipy.org/Cookbook/Matplotlib/Multiple_Subplots_with_One_Axis_Label?action=AttachFile&do=get&target=Same_ylabel_subplots.png)

although when I tried to do something similar with a 2x2 grid of plots it didn’t seem to be working for me. Their example works due to these lines here

import pylab
2
3 figprops = dict(figsize=(8., 8. / 1.618), dpi=128) # Figure properties
4 adjustprops = dict(left=0.1, bottom=0.1, right=0.97, top=0.93, wspace=0.2 hspace=0.2) # Subplot properties

5
6 fig = pylab.figure(**figprops) # New figure

8
9 ax = fig.add_subplot(3, 1, 1)
10 bx = fig.add_subplot(3, 1, 2, sharex=ax, sharey=ax)
11 cx = fig.add_subplot(3, 1, 3, sharex=ax, sharey=ax)
12
13 ax.plot([0,1,2], [2,3,4], ‘k-’)

14 bx.plot([0,1,2], [2,3,4], ‘k-’)
15 cx.plot([0,1,2], [2,3,4], ‘k-’)
16
17 pylab.setp(ax.get_xticklabels(), visible=False)
18 pylab.setp(bx.get_xticklabels(), visible=False)
19

20 bx.set_ylabel(‘This is a long label shared among more axes’, fontsize=14)
21 cx.set_xlabel(‘And a shared x label’, fontsize=14)

specifically probably the bx/cx_set label commands coupled with the sharex commands in the subplot label. But when I tried to add these things for my 2x2 plot it always looked like the label was attached to one of the plots or another instead of spanning the whole range.

Thanks,

···

On Tue, Sep 20, 2011 at 1:28 AM, Jeffrey Spencer <jeffspencerd@…287…> wrote:

``````Not sure what you mean global axis but I think I was trying to do
``````

something similar with this. This is the chunk of one subplot.
Specifically look at last three lines:

``````ax = fig.add_subplot(2,2,2)

ax.set_title('b) 5')

ax.set_ylim((0,yUpper))

for i in tempRun:

ax.plot(x,actSum[1,semi,i,semi], label=tempLabel[i],
``````

linestyle=dashs[i%len(dashs)], color=plotColor[i%len(plotColor)])

``````clear_spines(ax)

ax.set_xticklabels('')

ax.yaxis.set_ticks_position('left')

ax.xaxis.set_ticks_position('bottom')

Cheers,

Jeff
``````
``````On 20/09/11 09:20, Brad Malone wrote:
``````

Hello again,

``````    I've actually made some more progress on my last question
``````

after finding this site: http://www.scipy.org/Cookbook/Matplotlib/Multiple_Subplots_with_One_Axis_Label

and I was able to adjust the widths of my spacing.

My current plot looks like this:

http://imageshack.us/photo/my-images/849/current.png/

with source code that looks like:

``````      adjustprops = dict(left=0.1, bottom=0.1, right=0.97, top=0.93,
``````

wspace=0.25, hspace=0.25)

``````      fig=figure()

ax=subplot(221)

plot(x2cp,y2cp,'bo',markersize=3)

#ax.set_title('2cP')

text(12,0.03,'2cP')

#ylabel('Displacement')

axis([0,16,0,0.04])

bx=subplot(223)

plot(x2cb,y2cb,'bo',markersize=3)

#ax.set_title('2cB')

text(12,0.225,'2cB')

axis([0,16,0,0.3])

cx=subplot(222)

plot(x6fp,y6fp,'bo',markersize=3)

#ax.set_title('6fP')

text(12,0.03375,'6fP')

axis([0,16,0,0.045])

dx=subplot(224)

plot(x6fb,y6fb,'bo',markersize=3)

#ax.set_title('6fB')

text(12,0.225,'6fB')

axis([0,16,0,0.3])

show()
``````
``````      Now the only thing I'd like to do now is create a global
``````

y-axis and a global x-axis along the bottom for all 4 plots
but trying what I thought was equivalent didn’t work). And
lastly, I wanted those y-axes which go from 0.000 to 0.040 to
instead go from 0.00 to 0.04 (i.e., only two decimal places,
getting rid of the ones that are in between).

Thanks for any guidance you can give.

Best,

``````      On Mon, Sep 19, 2011 at 3:32 PM, Brad > > Malone <brad.malone@...287...> > >           wrote:
``````
``````        Hi, I am
``````

trying to do a simple 2x2 box of plots. My code is so far
very simple, and simply looks something like this.

ax=subplot(221)

plot(x2cp,y2cp)

``````              ax.set_title('2cP')

ylabel('Displacement')

ax=subplot(222)

plot(x2cb,y2cb)

ax.set_title('2cB')

ax=subplot(223)

plot(x6fp,y6fp)

ax.set_title('6fP')

ax=subplot(224)

plot(x6fb,y6fb)

ax.set_title('6fB')

show()
``````
``````          But when I do this, the text and numbers from various
``````

plots overlap each other and get in the way (I noticed
this is a common problem among the images in the
matplotlib gallery as well). I have 2 questions:

``````          1). How can I control the spacing, or padding, between
``````

the plots so that the numbers don’t overlap?

``````          2). Is there a way for me to have a single common
``````

y-axis label and x-axis label that runs along the full
left-hand side and bottom, respectively?

Thanks so much for the help!

``````------------------------------------------------------------------------------
All the data continuously generated in your IT infrastructure contains a
definitive record of customers, application performance, security
threats, fraudulent activity and more. Splunk takes this data and makes
sense of it. Business sense. IT sense. Common sense.
[http://p.sf.net/sfu/splunk-d2dcopy1](http://p.sf.net/sfu/splunk-d2dcopy1)
``````
``````_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
[https://lists.sourceforge.net/lists/listinfo/matplotlib-users](https://lists.sourceforge.net/lists/listinfo/matplotlib-users)
``````

Maybe the label_outer() function will be what you need. Call it for each subplot axes, and set the labels as you would normally. matplotlib would then set visible only the labels that are on the outside edge.

Another option is to use the AxesGrid1 toolkit, which makes these things easy. Also note that the soon-to-be released v1.1 will have a tight_layout() function that can help with your spacing issues.

Cheers!

Ben Root

···

On Tue, Sep 20, 2011 at 11:06 AM, Brad Malone <brad.malone@…287…> wrote:

Hi Jeffrey,

Thanks the response. Sorry about the term “global axis”. That was clearly not the best way to say it. What I meant to say is global x axis LABEL and y-axis LABEL. This can be seen in this example (http://www.scipy.org/Cookbook/Matplotlib/Multiple_Subplots_with_One_Axis_Label?action=AttachFile&do=get&target=Same_ylabel_subplots.png)

although when I tried to do something similar with a 2x2 grid of plots it didn’t seem to be working for me. Their example works due to these lines here

import pylab
2
3 figprops = dict(figsize=(8., 8. / 1.618), dpi=128) # Figure properties
4 adjustprops = dict(left=0.1, bottom=0.1, right=0.97, top=0.93, wspace=0.2 hspace=0.2) # Subplot properties

5
6 fig = pylab.figure(**figprops) # New figure

8
9 ax = fig.add_subplot(3, 1, 1)
10 bx = fig.add_subplot(3, 1, 2, sharex=ax, sharey=ax)
11 cx = fig.add_subplot(3, 1, 3, sharex=ax, sharey=ax)
12
13 ax.plot([0,1,2], [2,3,4], ‘k-’)

14 bx.plot([0,1,2], [2,3,4], ‘k-’)
15 cx.plot([0,1,2], [2,3,4], ‘k-’)
16
17 pylab.setp(ax.get_xticklabels(), visible=False)
18 pylab.setp(bx.get_xticklabels(), visible=False)
19

20 bx.set_ylabel(‘This is a long label shared among more axes’, fontsize=14)
21 cx.set_xlabel(‘And a shared x label’, fontsize=14)

specifically probably the bx/cx_set label commands coupled with the sharex commands in the subplot label. But when I tried to add these things for my 2x2 plot it always looked like the label was attached to one of the plots or another instead of spanning the whole range.

Thanks,

Hi Ben,

Thanks. Using label_outer() does, as you say, only show labels on the edge and that is something I wanted to do. It doesn’t, however, make a common y-axis label and a common x-axis label (instead there are now 2 of each, instead of 4). It appears that I might be able to add a common y and x label by brute-forcing the labels with text, along the lines of something like:

fig.text(0.5,0.04,‘common xlabel’,ha=‘center’,va=‘center’)
fig.text(0.00,0.5,‘common ylabel’,ha=‘center’,va=‘center’,rotation=‘vertical’)

Now I just need to play with spacing and change precise ticklabels and I’ll be able to finish this plot up!

Thanks everyone,

···

On Tue, Sep 20, 2011 at 9:12 AM, Benjamin Root <ben.root@…3203…04…> wrote:

On Tue, Sep 20, 2011 at 11:06 AM, Brad Malone <brad.malone@…287…> wrote:

Hi Jeffrey,

Thanks the response. Sorry about the term “global axis”. That was clearly not the best way to say it. What I meant to say is global x axis LABEL and y-axis LABEL. This can be seen in this example (http://www.scipy.org/Cookbook/Matplotlib/Multiple_Subplots_with_One_Axis_Label?action=AttachFile&do=get&target=Same_ylabel_subplots.png)

although when I tried to do something similar with a 2x2 grid of plots it didn’t seem to be working for me. Their example works due to these lines here

import pylab
2
3 figprops = dict(figsize=(8., 8. / 1.618), dpi=128) # Figure properties
4 adjustprops = dict(left=0.1, bottom=0.1, right=0.97, top=0.93, wspace=0.2 hspace=0.2) # Subplot properties

5
6 fig = pylab.figure(**figprops) # New figure

8
9 ax = fig.add_subplot(3, 1, 1)
10 bx = fig.add_subplot(3, 1, 2, sharex=ax, sharey=ax)
11 cx = fig.add_subplot(3, 1, 3, sharex=ax, sharey=ax)
12
13 ax.plot([0,1,2], [2,3,4], ‘k-’)

14 bx.plot([0,1,2], [2,3,4], ‘k-’)
15 cx.plot([0,1,2], [2,3,4], ‘k-’)
16
17 pylab.setp(ax.get_xticklabels(), visible=False)
18 pylab.setp(bx.get_xticklabels(), visible=False)
19

20 bx.set_ylabel(‘This is a long label shared among more axes’, fontsize=14)
21 cx.set_xlabel(‘And a shared x label’, fontsize=14)

specifically probably the bx/cx_set label commands coupled with the sharex commands in the subplot label. But when I tried to add these things for my 2x2 plot it always looked like the label was attached to one of the plots or another instead of spanning the whole range.

Thanks,