如何在Matplotlib中绘制同一图上的多个函数?

时间:2014-03-08 22:48:59

标签: python function matplotlib graph

如何在同一图中的域sin上绘制以下3个功能(即cost和添加)?

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

3 个答案:

答案 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()

enter image description here

答案 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()

enter image description here

答案 2 :(得分:4)

只需使用plot函数,如下所示

figure()
...
plot(t, a)
plot(t, b)
plot(t, c)