Setting size of subplot

Hello,

I am new to matplotlib and am having trouble understanding how to set
the size of a subplot when a figure contains multiple subplots. In
particular, I have been playing around with the scatter_hist.py demo
at http://matplotlib.sourceforge.net/examples/axes_grid/scatter_hist.html.
A simplified version of this code is given below:

···

*****************

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid import make_axes_locatable

fig = plt.figure(1, figsize=(4,4))

axScatter = plt.subplot(111)
divider = make_axes_locatable(axScatter)
axHisty = divider.new_horizontal(1.2, pad=0.5, sharey=axScatter)
fig.add_axes(axHisty)

x = np.random.randn(1000)
y = np.random.randn(1000)
axScatter.scatter(x, y)
axHisty.hist(x, orientation='horizontal')

plt.draw()
plt.show()

*****************

I'd like to have direct control over the size of the scatter plot in
this figure. As it stands, I can 'sort of' control its size by
changing the figsize property of the figure. I say 'sort of' since the
size of any labels also come into play here. Is it possible to
directly make the scatter plot a certain size (say, 3 x 2 inches), set
the figure size independently (say, 5 x 4 inches), and have the
histogram size be set based on the scatter plot height and width set
in divider.new_horizontal (in this case to 3 x 1.2 inches)?

I realize that in this example, it probably seems silly to not just
change figsize, but I am working with a more complicated plot in
reality where I'd like precise control over the size of the initial
subplot since the aspect ratio is important to me. I can then adjust
the figsize to make sure all the labels fit in nicely.

Thanks for any and all help.

Cheers,
Donovan

You will probably want to add axes explicitly (not with subplot), e.g.

fig.add_axes([.1,.1,.71,.8])

specifies the coordinates of one corner and the width and height (in proportions of the figure size). When doing this explicitly, you will probably need to do some extra adjustments to fit the axis labels and so forth.

&C

···

On Oct 5, 2009, at 5:59 PM, Donovan Parks wrote:

Hello,

I am new to matplotlib and am having trouble understanding how to set
the size of a subplot when a figure contains multiple subplots. In
particular, I have been playing around with the scatter_hist.py demo
at http://matplotlib.sourceforge.net/examples/axes_grid/scatter_hist.html.
A simplified version of this code is given below:

*****************

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid import make_axes_locatable

fig = plt.figure(1, figsize=(4,4))

axScatter = plt.subplot(111)
divider = make_axes_locatable(axScatter)
axHisty = divider.new_horizontal(1.2, pad=0.5, sharey=axScatter)
fig.add_axes(axHisty)

x = np.random.randn(1000)
y = np.random.randn(1000)
axScatter.scatter(x, y)
axHisty.hist(x, orientation='horizontal')

plt.draw()
plt.show()

*****************

I'd like to have direct control over the size of the scatter plot in
this figure. As it stands, I can 'sort of' control its size by
changing the figsize property of the figure. I say 'sort of' since the
size of any labels also come into play here. Is it possible to
directly make the scatter plot a certain size (say, 3 x 2 inches), set
the figure size independently (say, 5 x 4 inches), and have the
histogram size be set based on the scatter plot height and width set
in divider.new_horizontal (in this case to 3 x 1.2 inches)?

I realize that in this example, it probably seems silly to not just
change figsize, but I am working with a more complicated plot in
reality where I'd like precise control over the size of the initial
subplot since the aspect ratio is important to me. I can then adjust
the figsize to make sure all the labels fit in nicely.

Thanks for any and all help.

Cheers,
Donovan

------------------------------------------------------------------------------
Come build with us! The BlackBerry® Developer Conference in SF, CA
is the only developer event you need to attend this year. Jumpstart your
developing skills, take BlackBerry mobile applications to market and stay
ahead of the curve. Join us from November 9-12, 2009. Register now!
http://p.sf.net/sfu/devconf
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users

I just want to mention that axes size in mpl, by design, is supposed
to be proportional to the figure size, so there could be cases when a
fixed-sized axes messes up something. Furthermore, using axes_grid
toolkit can be a bit tricky, so I (as an author of the axes_grid
toolkit) personally do no recommend it for someone new to matplotlib.
Having said that, add below after the make_axes_locatable call.

from mpl_toolkits.axes_grid import Size
divider.get_horizontal()[0] = Size.Fixed(3.0) # 3 inch
divider.get_vertical()[0] = Size.Fixed(2.0) # 2 inch

The axes will have a fixed size regardless of the figure size. Also,
take a look at the below example of fixed size axes.

http://matplotlib.sourceforge.net/examples/axes_grid/demo_fixed_size_axes.html

FYI, if you want your output automatically clipped according to the
axes size and label etc., try savefig with bbox_inches="tight" option
(it does not affect the displayed figure size). Although the current
algorithm is not perfect, it seems to work okay for a simple case as
yours.

Regards,

-JJ

···

On Mon, Oct 5, 2009 at 8:59 PM, Donovan Parks <donovan.parks@...287...> wrote:

Hello,

I am new to matplotlib and am having trouble understanding how to set
the size of a subplot when a figure contains multiple subplots. In
particular, I have been playing around with the scatter_hist.py demo
at http://matplotlib.sourceforge.net/examples/axes_grid/scatter_hist.html.
A simplified version of this code is given below:

*****************

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid import make_axes_locatable

fig = plt.figure(1, figsize=(4,4))

axScatter = plt.subplot(111)
divider = make_axes_locatable(axScatter)
axHisty = divider.new_horizontal(1.2, pad=0.5, sharey=axScatter)
fig.add_axes(axHisty)

x = np.random.randn(1000)
y = np.random.randn(1000)
axScatter.scatter(x, y)
axHisty.hist(x, orientation='horizontal')

plt.draw()
plt.show()

*****************

I'd like to have direct control over the size of the scatter plot in
this figure. As it stands, I can 'sort of' control its size by
changing the figsize property of the figure. I say 'sort of' since the
size of any labels also come into play here. Is it possible to
directly make the scatter plot a certain size (say, 3 x 2 inches), set
the figure size independently (say, 5 x 4 inches), and have the
histogram size be set based on the scatter plot height and width set
in divider.new_horizontal (in this case to 3 x 1.2 inches)?

I realize that in this example, it probably seems silly to not just
change figsize, but I am working with a more complicated plot in
reality where I'd like precise control over the size of the initial
subplot since the aspect ratio is important to me. I can then adjust
the figsize to make sure all the labels fit in nicely.

Thanks for any and all help.

Cheers,
Donovan

------------------------------------------------------------------------------
Come build with us! The BlackBerry&reg; Developer Conference in SF, CA
is the only developer event you need to attend this year. Jumpstart your
developing skills, take BlackBerry mobile applications to market and stay
ahead of the curve. Join us from November 9&#45;12, 2009. Register now&#33;
http://p.sf.net/sfu/devconf
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users