title: Matplotlib: Adding & updating axes
body: <p>I'm interrested in writing a function that will display a whole array as well as linking two other axes to the mouse coordinates (pixel of coordinates (n,m), one axe will contain array[n,:] and the other array[:,m])</p>
  
  <p>I've made substantial progress but there is some issues that I can't solve and I would like some help!</p>
  
  <p>First: there is some white space between the axes and the image, I don't really know why they appear.
  Second: when I resize the figure, I would like to keep a square shape but I don't see how I can do this.
  Third: when I move the mouse in the figure, I can't update the two added axes (ax1 and ax2) like I want...</p>
  
  <p>For the third point, if I put ax1/ax2.cla() there will be some problem because there will be no more lines.
  fig.canvas.draw_idle() and plt.draw() seems to solve part of the problem but when the mouse is not moving ax1 and ax2 become empty...</p>
  
  <p>Below is a little snippet cornering the principal difficulties I've been encountering:</p>
  
  <pre><code>import matplotlib.pyplot as plt
  import numpy as np
  
  def update(event):
      if event.inaxes:
          a = ax1.get_lines()[0]
          b = ax2.get_lines()[0]
          a.set_xdata(data[:,int(event.ydata)])
          b.set_ydata(data[int(event.xdata),:])
  
          ax1.draw_artist(a)
          ax2.draw_artist(b)
          fig.canvas.blit(ax1.bbox)
          fig.canvas.blit(ax2.bbox)
  
  data = np.random.rand(200,200)*10000
  fig = plt.figure(num = None, figsize=(8, 8), dpi=60)
  ax = fig.add_axes([0.1, 0.1, 0.8, 0.8])
  ax.imshow(data, interpolation = "bilinear", origin = "lower", cmap = 'binary',aspect='auto')
  ax1 = fig.add_axes([0.9, 0.1, 0.05, 0.8], sharey = ax)
  ax2 = fig.add_axes([0.1, 0.05, 0.8, 0.05], sharex = ax)
  ax1.set_xlim((0,10000))
  ax2.set_ylim((0,10000))
  ax1.plot(np.arange(0,200,1)*50, np.arange(0,200,1), animated = True)
  ax2.plot(np.arange(0,200,1), np.arange(0,200,1)*50, animated = True)
  fig.canvas.mpl_connect("motion_notify_event", update)
  
  plt.show()
  </code></pre>
  
  <p>Thanks a lot!</p>
  
  <p>EDIT : This is the closest I can get to the result I wanted. I keeped the aspect = 'auto' because otherwise when figure is resized, the sub axes (ax1, ax2) do not keep the principal axe (ax) shape... so I prefer to have the ax take the size of ax1 and ax2. Maybe it there is an option for axes to have something like disabling aspect (like no aspect = 'auto' for imshow).</p>
  
  <pre><code>import matplotlib.pyplot as plt
  import numpy as np
  
  def update(event):
      if not event.inaxes: return
      a = ax1.get_lines()[0]
      b = ax2.get_lines()[0]
      a.set_xdata(data[:,int(event.xdata)])
      b.set_ydata(data[int(event.ydata),:])
      fig.canvas.draw()
  
  def axis_to_fig(axis):
      def transform(coord):
          return fig.transFigure.inverted().transform(
              axis.transAxes.transform(coord))
      return transform
  
  def add_sub_axes(axis, rect, share):
      fig = axis.figure
      left, bottom, width, height = rect
      trans = axis_to_fig(axis)
      figleft, figbottom = trans((left, bottom))
      figwidth, figheight = trans([width,height]) - trans([0,0])
      if share == "x":
          return fig.add_axes([figleft, figbottom, figwidth, figheight], sharex = axis)
      if share == "y":
          return fig.add_axes([figleft, figbottom, figwidth, figheight], sharey = axis)
  
  data = np.random.rand(200,200)*10000
  
  fig = plt.figure(num = None, figsize=(8, 8), dpi=60)
  ax = fig.add_subplot(111)#add_axes([0.1, 0.1, 0.8, 0.8])
  ax.imshow(data, interpolation = "bilinear", origin = "lower", cmap = 'binary', aspect='auto')
  ax.set_adjustable('box-forced')
  ax1 = add_sub_axes(ax, [1.0, 0, 0.1, 1], "y")
  ax2 = add_sub_axes(ax, [0, -0.1, 1, 0.1], "x")
  ax1.plot(np.arange(0,200,1)*50, np.arange(0,200,1), '-',color = 'red')
  ax1.grid(True)
  ax2.plot(np.arange(0,200,1), np.arange(0,200,1)*50, '-',color = 'red')
  ax2.grid(True)
  
  ax2.set_xlim((0,np.size(data,0)))
  ax1.set_ylim((0,np.size(data,1)))
  
  fig.canvas.mpl_connect("motion_notify_event", update)
  
  ax.axis('off')
  plt.show()
  </code></pre>
  
  <p>Still, the added axes do not match exactly the x/y axis of the initial subplot and that's sad... </p>