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);