提高ValueError(“x和y必须具有相同的第一个维度”)ValueError:x和y必须具有相同的第一个维度

时间:2015-11-09 03:22:08

标签: python arrays

Heun是我更新x和y值的地方。我得到了 “引发ValueError(”x和y必须具有相同的第一维“) ValueError:x和y必须具有相同的第一个维度“ 错误。我知道我的数组有一个问题,并且通过在xposition的方括号中加1,并且在yposition,同时运行,然后运行,然后通过在每个数组的前面粘贴任意值来尝试弄乱它们的长度每一个并运行,但我仍然不知道错误是什么试图告诉我或如何解决它。我有它打印xposition和yposition进行小迭代(20),但它们都有20的长度。我也试图让它打印xposition.shape [0]和yposition.shape [0],但它说它们没有形状属性,即使错误是说问题是x.shape [0]!= y.shape [0]

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\Tyler\Anaconda\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 685, in runfile
    execfile(filename, namespace)
  File "C:\Users\Tyler\Anaconda\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 71, in execfile
    exec(compile(scripttext, filename, 'exec'), glob, loc)
  File "C:/Users/Tyler/Documents/Computational Physics/proj8.py", line 117, in <module>
    planet()
  File "C:/Users/Tyler/Documents/Computational Physics/proj8.py", line 115, in planet
    orbs = orbitx(r0,v0,Nstep,dt)
  File "C:/Users/Tyler/Documents/Computational Physics/proj8.py", line 101, in orbitx
    axarr[1,0].plot(times,Energy,times,kinetic,times,potential)
  File "C:\Users\Tyler\Anaconda\lib\site-packages\matplotlib\axes\_axes.py", line 1373, in plot
    for line in self._get_lines(*args, **kwargs):
  File "C:\Users\Tyler\Anaconda\lib\site-packages\matplotlib\axes\_base.py", line 313, in _grab_next_args
    for seg in self._plot_args(remaining[:isplit], kwargs):
  File "C:\Users\Tyler\Anaconda\lib\site-packages\matplotlib\axes\_base.py", line 282, in _plot_args
    x, y = self._xy_from_xy(x, y)
  File "C:\Users\Tyler\Anaconda\lib\site-packages\matplotlib\axes\_base.py", line 223, in _xy_from_xy
    raise ValueError("x and y must have same first dimension")
ValueError: x and y must have same first dimension

times = []
xposition=[]
yposition=[] 
xvelocity=[]
yvelocity=[]
ang_mom=[]
r_polar = []
theta_polar = []
kinetic = []
potential = []
E = []
#main loop to call Heun over and over again.  
for i in range(Nstep):   

    x,y,vx,vy = Heun(dt,x,y,vx,vy)  #note here is where do the Heun step        
    radius = np.sqrt(x**2 + y**2)       # you will need this quantity for a few things
    velocity = np.sqrt(vx**2 + vy**2) # you will need the magnitude of v for kinetic energy

    time += dt         #update time
    times.append(time) #store time       

    L = np.cross([x,y,0],[vx,vy,0])

    U = GM / radius

    KE = .5 * velocity**2 

    Energy = KE + U
    #the following keeps track of rmax and rmin
    if radius < r_min:
        r_min = radius
    elif radius > r_max:
        r_max = radius

    #now we store the information
    xposition.append(x)
    yposition.append(y)
    xvelocity.append(vx)
    yvelocity.append(vy)
    r_polar.append(radius)               #keeps track of r for plotting in polar coordinates
    theta_polar.append(math.atan2(y,x))  #keeps track of theta for polar coordinates
    kinetic.append(KE)
    potential.append(U)
    ang_mom.append(L)
    E.append(Energy)    
print xposition
print yposition
###create an array of the total energy at each step#####
#some plotting examples.  You do not need to do the plots like this
fig1,axarr = py.subplots(2,2)
axarr[0,0].plot(xposition,yposition)
axarr[0,0].set_title('two body orbit')
axarr[1,0].plot(times,Energy,times,kinetic,times,potential)
axarr[1,0].legend(['Total E','Kinetic', 'Potential'],loc=7)
axarr[1,0].set_title('Energy vs Time')
axarr[1,1].set_title('x position vs time')
py.subplot(222,polar=True)
py.plot(theta_polar,r_polar)
axarr[1,1].plot(times,xposition)

1 个答案:

答案 0 :(得分:0)

在我的行中:

axarr [1,0] .plot(倍,能量,时间,动力学,时间,电势)

我试图绘制能量,但是E是我的阵列,因为它是:

E = []

能量= KE + U

和,

E.append(能源)

我只需将行更改为: axarr [1,0] .plot(倍,E,倍能,动能倍,电位)