embedding figures inside another (coordinates)

Hello,

I am new to matplotlib and pylab.
I have an image plotted with imshow/contour, or even just a coordinate system plotted with axis.

I need to plot smaller contours/images on them at selected pixels. The only way I can figure how to do that is using axes. However, axes is defined on normalised coordinates which cover the entire window, whereas the axis of the main figure is drawn within it, with some margin.

My problem is how to get the correct coordinates for plotting each of the smaller figures ? The easiest way to do so would be to get the normalised coordinates of the rectangle which is drawn by axis. Is there a way to get that?

Thanks
Mohan.

This thread might be helpful.

http://thread.gmane.org/gmane.comp.python.matplotlib.general/16373

Take a look at the above thread and see if it fits your need.

However, it became tricky if your axes adjust its position (e.g.,
aspect=1) during the drawing time.
The example below will be helpful in those case (you need recent
version of matplotlib for this, see if your axes has
"set_axes_locator" method), but this requires some knowledge on how
mpl works.

import matplotlib.transforms

class InsetPosition(object):
    def __init__(self, parent, lbwh):
        self.parent = parent
        self.lbwh = lbwh # position of the inset axes in the
normalized coordinate of the parent axes

    def __call__(self, ax, renderer):
        bbox_parent = self.parent.get_position(original=False)
        trans = matplotlib.transforms.BboxTransformTo(bbox_parent)
        bbox_inset = matplotlib.transforms.Bbox.from_bounds(*self.lbwh)
        bb = matplotlib.transforms.TransformedBbox(bbox_inset, trans)
        return bb

ax = gca()
ax.set_aspect(1.)
axins = axes([0, 0, 1, 1])
ip = InsetPosition(ax, [0.5, 0.1, 0.4, 0.2])
axins.set_axes_locator(ip)

IHTH,

-JJ

···

On Wed, Apr 1, 2009 at 8:22 AM, oyarsa the old <oyarsa42@...287...> wrote:

Hello,

I am new to matplotlib and pylab.
I have an image plotted with imshow/contour, or even just a coordinate
system plotted with axis.

I need to plot smaller contours/images on them at selected pixels. The only
way I can figure how to do that is using axes. However, axes is defined on
normalised coordinates which cover the entire window, whereas the axis of
the main figure is drawn within it, with some margin.

My problem is how to get the correct coordinates for plotting each of the
smaller figures ? The easiest way to do so would be to get the normalised
coordinates of the rectangle which is drawn by axis. Is there a way to get
that?

Thanks
Mohan.

------------------------------------------------------------------------------

_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users

Hi,

Thanks for your reply ! The thread you pointed me to, was useful, though I didnt uderstand everything in it yet.
However, I found that the following works for what I want … though it does what I want, am I making some horrible mistake which will come around and haunt me later ?

plsub = subplot(1,1,1)
plaxis = axis([0, n 0, m])
start = plsub._position.p0
stop = plsub._position.p1

plsub._position.p0 and p1 store the normalised coordinates of the subplot box, it seems, which is what I need for now.

thanks!
Mohan

···

On Wed, Apr 1, 2009 at 7:54 PM, Jae-Joon Lee <lee.j.joon@…287…> wrote:

This thread might be helpful.

http://thread.gmane.org/gmane.comp.python.matplotlib.general/16373

Take a look at the above thread and see if it fits your need.

However, it became tricky if your axes adjust its position (e.g.,

aspect=1) during the drawing time.

The example below will be helpful in those case (you need recent

version of matplotlib for this, see if your axes has

“set_axes_locator” method), but this requires some knowledge on how

mpl works.

import matplotlib.transforms

class InsetPosition(object):

def __init__(self, parent, lbwh):

    self.parent = parent

    self.lbwh = lbwh # position of the inset axes in the

normalized coordinate of the parent axes

def __call__(self, ax, renderer):

    bbox_parent = self.parent.get_position(original=False)

    trans = matplotlib.transforms.BboxTransformTo(bbox_parent)

    bbox_inset = matplotlib.transforms.Bbox.from_bounds(*self.lbwh)

    bb = matplotlib.transforms.TransformedBbox(bbox_inset, trans)

    return bb

ax = gca()

ax.set_aspect(1.)

axins = axes([0, 0, 1, 1])

ip = InsetPosition(ax, [0.5, 0.1, 0.4, 0.2])

axins.set_axes_locator(ip)

IHTH,

-JJ

On Wed, Apr 1, 2009 at 8:22 AM, oyarsa the old <oyarsa42@…287…> wrote:

Hello,

I am new to matplotlib and pylab.

I have an image plotted with imshow/contour, or even just a coordinate

system plotted with axis.

I need to plot smaller contours/images on them at selected pixels. The only

way I can figure how to do that is using axes. However, axes is defined on

normalised coordinates which cover the entire window, whereas the axis of

the main figure is drawn within it, with some margin.

My problem is how to get the correct coordinates for plotting each of the

smaller figures ? The easiest way to do so would be to get the normalised

coordinates of the rectangle which is drawn by axis. Is there a way to get

that?

Thanks

Mohan.



Matplotlib-users mailing list

Matplotlib-users@lists.sourceforge.net

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

You are discouraged from using leading underscore attributes like
'_position' since the leading underscore designates that they are for
internal use and thus not protected against API changes. The public
interface is:

In [216]: ax = subplot(111)

In [217]: p = ax.get_position ()

In [218]: print p
Bbox(array([[ 0.125, 0.1 ],
       [ 0.9 , 0.9 ]]))

In [219]: p.xmin
Out[219]: 0.125

JDH

···

On Thu, Apr 2, 2009 at 6:47 AM, oyarsa the old <oyarsa42@...287...> wrote:

Hi,

Thanks for your reply ! The thread you pointed me to, was useful, though I
didnt uderstand everything in it yet.
However, I found that the following works for what I want ... though it does
what I want, am I making some horrible mistake which will come around and
haunt me later ?

plsub = subplot(1,1,1)
plaxis = axis([0, n 0, m])
start = plsub._position.p0
stop = plsub._position.p1

plsub._position.p0 and p1 store the normalised coordinates of the subplot
box, it seems, which is what I need for now.