如何在同一图中使python(matplotlib)中的两个图不同

问题描述:

我写了一个简单的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() 

一种方法是改变颜色和减少一个情节的不透明度:

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

谢谢,它看起来好多了。 –

你可以调用plot方法两次,像

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

感谢,但仍曲线不看是明显的,在所有。 –