0

I have a matplotlib code for creating a set of three subplots which share the same axes. My problem is that I am trying to plot an ellipse on each of these subplots, but the ellipse only shows up on one of the subplots. Can anyone tell me what I'm doing wrong?

Sorry if this kind of thing has already been answered elsewhere, I've been looking for a while but can't find an answer!

from pylab import *
import numpy as np
import sys 
import matplotlib.pyplot as plt
import matplotlib.cm as cm
import scipy.interpolate
import matplotlib
from matplotlib.patches import Ellipse
from numpy import linspace
from scipy import pi,sin,cos

data = np.genfromtxt(sys.argv[1]);
name1 = (sys.argv[1]);
name = ( sys.argv[2] );
print(sys.argv)

font = {'family' : 'normal',
    'size'   : 16}

matplotlib.rc('font', **font)



def ellipse(ra,rb,ang,x0,y0,Nb=50):
xpos,ypos=x0,y0
radm,radn=ra,rb
an=ang

co,si=cos(an),sin(an)
the=linspace(0,2*pi,Nb)
X=radm*cos(the)*co-si*radn*sin(the)+xpos
Y=radm*cos(the)*si+co*radn*sin(the)+ypos
return X,Y

def plot(x, y, z, name1, name):

#    I2 = scipy.interpolate.NearestNDInterpolator((x, y), z)
  I2 = scipy.interpolate.Rbf(x, y, z, function='linear')


 xi, yi = np.linspace(x.min(), x.max(), 100), np.linspace(y.min(), y.max(), 100)
 xig, yig = np.meshgrid(xi, yi)
 zi     = I2(xig, yig)
 plt.clf()

#creating plot

 f, axarr = plt.subplots(1,3,sharey=True)
 plt.setp(axarr.flat, aspect=1.0, adjustable='box-forced')

#first plot

  im1=axarr[2].imshow(zi, vmin=0, vmax=1,cmap='gist_heat_r', origin='lower', extent=[x.min(), x.max(), y.min(), y.max()])
#    axarr[2].set_title('Sharing both axes')
  X,Y=ellipse(7.36,2.29,0,0,0,Nb=70)
  plt.plot(X,Y,"g.-",ms=1) # green ellipse

#second plot
  im2=axarr[1].imshow(zi, vmin=0, vmax=1,cmap='gist_heat_r', origin='lower', extent=[x.min(), x.max(), y.min(), y.max()])
  X,Y=ellipse(7.36,2.29,0,0,0,Nb=70)
  plt.plot(X,Y,"g.-",ms=1) # green ellipse

#third plot    
 im3=axarr[0].imshow(zi, vmin=0, vmax=1,cmap='gist_heat_r', origin='lower', extent=[x.min(), x.max(), y.min(), y.max()])

# axis labels
 plt.xlabel('X AXIS (kpc)')
 plt.ylabel('Y AXIS (kpc)')

 f.subplots_adjust(hspace=0);
 f.subplots_adjust(wspace=0);

 X,Y=ellipse(7.36,2.29,0,0,0,Nb=70)
 plt.plot(X,Y,"g.-",ms=1) # green ellipse

# Colorbar    
  from mpl_toolkits.axes_grid1 import make_axes_locatable
  divider = make_axes_locatable(plt.gca())
  cax = divider.append_axes("right", "5%", pad="3%")
  plt.colorbar(im1,cax=cax)

# Save figure
  plt.savefig(name1 + "_norm.eps", bbox_inches='tight');

  plot(data[:,0], data[:,1], data[:,2], name1, name);
  • See http://stackoverflow.com/questions/16849483/which-is-the-recommended-way-to-plot-matplotlib-or-pylab/16849816#16849816 – tacaswell May 09 '14 at 13:16
  • and http://stackoverflow.com/questions/15858192/how-to-set-xlim-and-ylim-for-a-subplot-in-matplotlib/15858264#15858264 – tacaswell May 09 '14 at 13:17
  • possible duplicate of [How can I attach a pyplot function to a figure instance?](http://stackoverflow.com/questions/14254379/how-can-i-attach-a-pyplot-function-to-a-figure-instance) – tacaswell May 09 '14 at 13:19

1 Answers1

0

When you call plt.plot() on what axes do you expect it to be plotted on? It will plot on the current active axes, which in your case is probably the first.

You need to either change the current active axes with plt.sca(axarr[n]) before calling plt.plot(), or even better, stop mixing both the OO- and state machine interface and use axarr[n].plot().

You are using the OO-interface for .imshow(), so why not for .plot() as well?

Rutger Kassies
  • 61,630
  • 17
  • 112
  • 97