# aligning plot width with imshow/contour width

Hi List,

How can I make a plot have the same width as an image? The image (and
contour) have a fixed aspect ratio defined by the data. Now I'd like to
plot something under that image, with the same width.

I think this is trivial. I think I've done this in the past. But I
cannot find the code. Searching online and with experiments I've looked
into "sharex", and tried using "extent" and the bbox,
ax.transData.inverted(), etc. but I have not had any success, hence my
post here.

The following code demonstrates the problem. The 2nd axis is wider than
the first that contains the image.

Thanks,

-k.

fig = plt.figure(1)
fig.clf()

data = np.random.random((3,3))
xaxis = np.arange(0,3)
yaxis = np.arange(0,3)

im = ax.imshow(data, interpolation='none')
c = ax.contour(xaxis, yaxis, data, colors='k')

from matplotlib.pylab import *
fig = plt.figure(1)
fig.clf()
data = np.random.random((3,3))
xaxis = np.arange(0,3)
yaxis = np.arange(0,3)
im = ax1.imshow(data, interpolation='none')
c = ax1.contour(xaxis, yaxis, data, colors='k')

···

#----------------
# draw ax1 first, so you can get proper position of ax1
fig.canvas.draw()
point_bl, point_tr = ax1.get_position().get_points() # ax1's bottom-left
and top-right
space = 0.45 # space between ax2's bottom left and ax1's bottom left in
percentage
ax2.set_position([point_bl[0], point_bl[1]-space,
point_tr[0] - point_bl[0], point_tr[1] - point_bl[1]])
plt.show()

You can adjust that variable "space" as you need.

If someone has some more easy ways to do this, please let me know.

--
View this message in context: http://matplotlib.1069221.n5.nabble.com/aligning-plot-width-with-imshow-contour-width-tp44408p44409.html
Sent from the matplotlib - users mailing list archive at Nabble.com.

Hi Zhangtao,

When I run that code I do not see the 2nd subplot. If I add “fig.canvas.draw()” to the bottom, it appears, but it is not the same width as the upper subplot.

···

On Tue, Nov 18, 2014 at 10:15 AM, zhangtao <tao.quiver@…287…> wrote:

from matplotlib.pylab import *

fig = plt.figure(1)

fig.clf()

data = np.random.random((3,3))

xaxis = np.arange(0,3)

yaxis = np.arange(0,3)

im = ax1.imshow(data, interpolation=‘none’)

c = ax1.contour(xaxis, yaxis, data, colors=‘k’)

#----------------

# draw ax1 first, so you can get proper position of ax1

fig.canvas.draw()

point_bl, point_tr = ax1.get_position().get_points() # ax1’s bottom-left

and top-right

space = 0.45 # space between ax2’s bottom left and ax1’s bottom left in

percentage

ax2.set_position([point_bl[0], point_bl[1]-space,

``````              point_tr[0] - point_bl[0], point_tr[1] - point_bl[1]])
``````

plt.show()

You can adjust that variable “space” as you need.

If someone has some more easy ways to do this, please let me know.

View this message in context: http://matplotlib.1069221.n5.nabble.com/aligning-plot-width-with-imshow-contour-width-tp44408p44409.html

Sent from the matplotlib - users mailing list archive at Nabble.com.

with Interactivity, Sharing, Native Excel Exports, App Integration & more

Get technology previously reserved for billion-dollar corporations, FREE

Matplotlib-users mailing list

Matplotlib-users@lists.sourceforge.net

https://lists.sourceforge.net/lists/listinfo/matplotlib-users

I use matolotlib 1.3.1 on windows, the code works fine here.
<http://matplotlib.1069221.n5.nabble.com/file/n44444/figure_1.png>

what version of matolotlib are you using?

Or, set space to 0, see what happens.

···

--
View this message in context: http://matplotlib.1069221.n5.nabble.com/aligning-plot-width-with-imshow-contour-width-tp44408p44444.html
Sent from the matplotlib - users mailing list archive at Nabble.com.

I'm using mpl 1.4.2.

I posted this question onto StackOverflow and got a nice reply/tutorial.
https://stackoverflow.com/questions/26985210/

Thanks,

-k.