Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating square subplots (of equal height and width) in matplotlib

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 insists on auto-scaling.

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?

like image 920
gotgenes Avatar asked Jul 08 '10 20:07

gotgenes


People also ask

How do I make a square plot in Matplotlib?

We can set the aspect ratio of a plot using the set_aspect() method to make it a square plot and axis() method can also be used to make a square plot with equal axes in Matplotlib.

What does PLT subplot 1 2 1 mean?

The subplot() Function The layout is organized in rows and columns, which are represented by the first and second argument. The third argument represents the index of the current plot. plt.subplot(1, 2, 1) #the figure has 1 row, 2 columns, and this plot is the first plot.

How does subplot change figure size?

To change figure size of more subplots you can use plt. subplots(2,2,figsize=(10,10)) when creating subplots.


2 Answers

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. For example, you could 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() 

However, a better workaround is to change 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).

However, there's a third option for adjustable to handle exactly this case: adjustable="box-forced".

For example:

from pylab import *  figure() ax1 = subplot(121, aspect='equal', adjustable='box-forced') plot([1, 2, 3], [1, 2, 3]) subplot(122, aspect='equal', adjustable='box-forced', sharex=ax1, sharey=ax1) plot([1, 2, 3], [1, 2, 3]) show() 

Or in more modern style (note: this part of the answer wouldn't have worked in 2010):

import matplotlib.pyplot as plt  fig, axes = plt.subplots(ncols=2, sharex=True, sharey=True) for ax in axes:     ax.plot([1, 2, 3], [1, 2, 3])     ax.set(adjustable='box-forced', aspect='equal')  plt.show() 

Either way, you'll get something similar to:

enter image description here

like image 174
Joe Kington Avatar answered Sep 19 '22 23:09

Joe Kington


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 :)

like image 34
Kit Avatar answered Sep 22 '22 23:09

Kit