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
import matplotlib.pyplot as plt

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
  compTrans = ax.transData + ax.get_figure().transFigure.inverted()

  # 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.
  width=0.1
  height=0.1
  x,y = compTrans.transform(ij)
  bbox = [x-width/2.,y-height/2.,width,height]
  ax2 = plt.axes(bbox,axisbg='none')
  ax2.plot(0.5,0.5,'go')
  ax2.set_xlim((0,1))
  ax2.set_ylim((0,1))
  
  return ax2

def alter_appearance(ax,ij):
  ax.set_xlim((0,10))
  ax.set_ylim((0,10))
  ax.axhline(y=ij[1],color='0.7')
  ax.axvline(x=ij[0],color='0.7')

ij = (7,5)

f = plt.gcf()
f.add_subplot(211)
plt.plot(ij[0],ij[1],'ro')
ax = plt.gca()
ax.set_title('Auto aspect ratio')
alter_appearance(ax,ij)
add_inset(ax,ij)

f.add_subplot(212)
plt.plot(ij[0],ij[1],'ro')
ax = plt.gca()
ax.set_title('Fixed aspect ratio')
ax.set_aspect(1.5,adjustable='box')
alter_appearance(ax,ij)
add_inset(ax,ij)

plt.show()
------------------------------------------------------------------------------
Download Intel® 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

Reply via email to