简体   繁体   English

使用子图时的plt.plot

[英]plt.plot when using subplots

I have a matplotlib code for creating a set of three subplots which share the same axes. 我有一个matplotlib代码,用于创建一组共享相同轴的三个子图。 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);

When you call plt.plot() on what axes do you expect it to be plotted on? 当您在哪个轴上调用plt.plot() ,您希望将其绘制在哪个轴上? 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() . 您需要在调用plt.plot() plt.sca(axarr[n])之前使用plt.sca(axarr[n])更改当前活动轴,甚至更好的是,停止混合OO-和状态机接口,并使用axarr[n].plot()

You are using the OO-interface for .imshow() , so why not for .plot() as well? 您正在将OO接口用于.imshow() ,那么为什么不也将其用于.plot()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM