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

时间:2017-05-29 04:19:37

标签: python matplotlib

我已经编写了一个简单的python程序来使用Euler方法和Analytical方法解决简谐振子,但似乎两条曲线完全匹配(我不知道如何以及为什么?,因为它们必须不同)。由于这些曲线完美匹配,我无法区分这两条曲线。即使它们适合,有没有办法使用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()

2 个答案:

答案 0 :(得分:0)

你可以调用plot方法两次,比如

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

答案 1 :(得分: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