Transform problem with fixed aspect ratio

Hi list -

I’ve got a need to add several axes instances (e.g., barplots) to an existing figure. These additional axes need to be placed relative to data points on that plot.

A problem occurs if the aspect ratio of the parent axes is set to a fixed value rather than using ‘auto’.

The attached script illustrates the problem with a pair of plots. Both plot a single red dot on the parent figure. A subplot is then placed directly on top of this point and a single point is plotted there, in green. If all’s well you shouldn’t see the red dot and should just see the green dot. In figure 2, where the aspect ratio is fixed, the subplot does not get placed at the correct location.

Have I stumbled onto a subtlety of transforms that I’m not accounting for or is this perhaps a bug?

matplotlib version 0.99.1 on linux; numpy 1.2.1

thanks a lot,
-mike

insets.py (1.35 KB)

With axes aspect_ratio set, the position of the axes is determined
during the drawing time. And the position of the inset axes also need
to be adjusted during the drawing time to incorporate this,
One way to archive this is to set axes_locator attribute, which accept
a callable object that returns the position of the axes.

Here is a slightly modified version of your add_inset.

def add_inset(ax,ij):
  # Define a composite transform to go from data space in the Axes
  # instance to figure coordinates.
  # We need to perform a composite transform to get the location:
  # data to display followed by display to figure (because axes())
  # takes a rect for positions relative to the figure

  def my_axes_locator(axes, renderer):
    compTrans = ax.transData + ax.get_figure().transFigure.inverted()
    width=0.1
    height=0.1
    x,y = compTrans.transform(ij)
    bbox = [x-width/2.,y-height/2.,width,height]
    return bbox

  # Place a subplot centered on the specified location with a fixed size
  # (relative to the size of the figure)
  # The green dot we plot here should cover up the red dot on the plot
  # underneath this one. We have a problem if they don't align.
  ax2 = plt.axes([0,0,1,1],axisbg='none', axes_locator=my_axes_locator)

  ax2.plot(0.5,0.5,'go')
  ax2.set_xlim((0,1))
  ax2.set_ylim((0,1))

  return ax2

Regards,

-JJ

···

On Thu, Mar 4, 2010 at 12:53 PM, Mike Kay <vdotdel@...287...> wrote:

Hi list -

I've got a need to add several axes instances (e.g., barplots) to an
existing figure. These additional axes need to be placed relative to data
points on that plot.

A problem occurs if the aspect ratio of the parent axes is set to a fixed
value rather than using 'auto'.

The attached script illustrates the problem with a pair of plots. Both plot
a single red dot on the parent figure. A subplot is then placed directly on
top of this point and a single point is plotted there, in green. If all's
well you shouldn't see the red dot and should just see the green dot. In
figure 2, where the aspect ratio is fixed, the subplot does not get placed
at the correct location.

Have I stumbled onto a subtlety of transforms that I'm not accounting for or
is this perhaps a bug?

matplotlib version 0.99.1 on linux; numpy 1.2.1

thanks a lot,
-mike

------------------------------------------------------------------------------
Download Intel&#174; Parallel Studio Eval
Try the new software tools for yourself. Speed compiling, find bugs
proactively, and fine-tune applications for parallel performance.
See why Intel Parallel Studio got high marks during beta.
http://p.sf.net/sfu/intel-sw-dev
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users