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

时间:2021-04-03 16:50:07

How could I plot the following 3 functions (i.e. sin, cos and the addition), on the domain t, on the same figure?

我怎样才能在同一个数字的域t上绘制以下3个函数(即sin,cos和加法)?

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 个解决方案

#1


75  

To plot multiple graphs on the same figure you will have to do:

要在同一图上绘制多个图形,您必须执行以下操作:

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

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

#2


24  

Perhaps a more pythonic way of doing so.

这可能是一种更加pythonic的方式。

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

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

#3


4  

Just use the function plot as follows

只需使用功能图如下

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

#1


75  

To plot multiple graphs on the same figure you will have to do:

要在同一图上绘制多个图形,您必须执行以下操作:

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

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

#2


24  

Perhaps a more pythonic way of doing so.

这可能是一种更加pythonic的方式。

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

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

#3


4  

Just use the function plot as follows

只需使用功能图如下

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