2017-05-29 121 views
2

我寫了一個簡單的python程序來解決使用Euler方法和Analytical方法的簡單諧波振盪器,但似乎兩條曲線完全吻合(我不是確定如何以及爲什麼?因爲它們必須是不同的)。由於這些曲線非常合適,因此我無法對這兩條曲線進行區分。即使它們合適,是否有任何方法可以使用matplotlib的功能使它們不同。由於如何在同一圖中使python(matplotlib)中的兩個圖不同

import matplotlib.pyplot as plt 
import math as m 
g=9.8 
v=0.0 #initial velocity 
h=0.01 #time step 
x=5.0 #initial position 
w=m.sqrt(10.0) 

t=0.0 
ta,xa,xb=[],[],[] 

while t<12.0: 
    ta.append(t) 
    xa.append(x) 
    xb.append(5*m.cos(w*t)) 

    v=v-(10.0/1.0)*x*h #k=10.0, m=1.0 
    x=x+v*h 
    t=t+h 
plt.figure() 
plt.plot(ta,xa,ta,xb,'bo--') 
plt.xlabel('$t(s)$') 
plt.ylabel('$x(m)$') 
plt.show() 

回答

0

一種方法是改變顏色和減少一個情節的不透明度:

plt.plot(ta,xa) 
plt.plot(ta,xb,c='red',alpha=.5) 

代替:

plt.plot(ta,xa,ta,xb,'bo--') 

enter image description here

當縮放:enter image description here

您也可以撒一個並繪製其他:

plt.plot(ta,xa) 
plt.scatter(ta,xb,c='red',alpha=.3) 

enter image description here

+0

謝謝,它看起來好多了。 –

0

你可以調用plot方法兩次,像

plt.plot(ta, xa, 'bo--') 
plt.plot(ta, xb, 'gs') 
+0

感謝,但仍曲線不看是明顯的,在所有。 –

相關問題