views:

85

answers:

2

When I run this code

from pylab import *

figure()
ax1 = subplot(121)
plot([1, 2, 3], [1, 2, 3])
subplot(122, sharex=ax1, sharey=ax1)
plot([1, 2, 3], [1, 2, 3])
draw()
show()

I get two subplots which are "squished" in the X-dimension. How do I get these subplots such that the height of the Y-axis equals the width of the X-axis, for both subplots?

I am using matplotlib v.0.99.1.2 on Ubuntu 10.04.

Update 2010-07-08: Let's look at some things that don't work.

After Googling around all day, I thought that it might be related to auto-scaling. So I tried fiddling with that.

from pylab import *

figure()
ax1 = subplot(121, autoscale_on=False)
plot([1, 2, 3], [1, 2, 3])
subplot(122, sharex=ax1, sharey=ax1)
plot([1, 2, 3], [1, 2, 3])
draw()
show()

matplotlib keeps on auto-scaling, like a retarded champ.

from pylab import *

figure()
ax1 = subplot(121, autoscale_on=False)
plot([1, 2, 3], [1, 2, 3])
subplot(122, sharex=ax1, sharey=ax1, autoscale_on=False)
plot([1, 2, 3], [1, 2, 3])
draw()
show()

In this one, the data completely disappears. WTF, matplotlib? Just WTF?

Okay, well maybe if we fix the aspect ratio?

from pylab import *

figure()
ax1 = subplot(121, autoscale_on=False)
plot([1, 2, 3], [1, 2, 3])
axes().set_aspect('equal')
subplot(122, sharex=ax1, sharey=ax1)
plot([1, 2, 3], [1, 2, 3])
draw()
show()

This one causes the first subplot to disappear entirely. That's hilarious! Who came up with that one?

In all seriousness, now... should this really be such a hard thing to accomplish?

+2  A: 

Give this a try:

from pylab import *

figure()
ax1 = subplot(121, autoscale_on=False, aspect='equal', xlim=[1,3], ylim=[1,3])
plot([1, 2, 3], [1, 2, 3])
##axes().set_aspect('equal')
ax2 = subplot(122, autoscale_on=False, aspect='equal', xlim=[1,3], ylim=[1,3])
plot([1, 2, 3], [1, 2, 3])
draw()
show()

I commented out the axes() line as that would create a new axes at an arbitrary location, rather than a pre-fabricated subplot with a calculated position.

Calling subplot actually creates an Axes instance, which means it can use the same properties as that of an Axes.

I hope this helps :)

Kit
+2  A: 

Your problem in setting the aspect of the plots is coming in when you're using sharex and sharey. One workaround is to just not used shared axes.

The behavior you're looking for is the "adjustable" keywarg... You want adjustable='box', but when you're using shared axes, it has to be adjustable='datalim' (and setting it back to 'box' gives an error).

For example, if you just do this:

from pylab import *

figure()
subplot(121, aspect='equal')
plot([1, 2, 3], [1, 2, 3])
subplot(122, aspect='equal')
plot([1, 2, 3], [1, 2, 3])
show()

You get this: Square Subplots

But of course your axes aren't linked, so zooming in on one won't affect the other, as it would have in your original example.

If you make the axes shared, the above won't work, as axes.adjustable will be set back to 'datalim', and the boxes won't be square. This is a known "feature"/bug, and I think (?) it may have been fixed in svn (I've seen a lot of posts on matplotlib-users about it, and I vaguely recall hearing that it had been fixed).

Joe Kington