如何在同一图中的域sin
上绘制以下3个功能(即cos
,t
和添加)?
from numpy import *
import math
import matplotlib.pyplot as plt
t = linspace(0, 2*math.pi, 400)
a = sin(t)
b = cos(t)
c = a + b
答案 0 :(得分:118)
要在同一图上绘制多个图形,您必须执行以下操作:
from numpy import *
import math
import matplotlib.pyplot as plt
t = linspace(0, 2*math.pi, 400)
a = sin(t)
b = cos(t)
c = a + b
plt.plot(t, a, 'r') # plotting t, a separately
plt.plot(t, b, 'b') # plotting t, b separately
plt.plot(t, c, 'g') # plotting t, c separately
plt.show()
答案 1 :(得分:36)
这可能是一种更加抒情的方式。
from numpy import *
import math
import matplotlib.pyplot as plt
t = linspace(0,2*math.pi,400)
a = sin(t)
b = cos(t)
c = a + b
plt.plot(t, a, t, b, t, c)
plt.show()
答案 2 :(得分:4)
只需使用plot
函数,如下所示
figure()
...
plot(t, a)
plot(t, b)
plot(t, c)