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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22276066/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 00:38:58  来源:igfitidea点击:

How to plot multiple functions on the same figure, in Matplotlib?

pythonfunctionmatplotlibgraph

提问by user3277335

How can I plot the following 3 functions (i.e. sin, cosand the addition), on the domain t, in the same figure?

如何在同一个图中的域上绘制以下 3 个函数(即sincos和加法)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

采纳答案by ThePredator

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

enter image description here

在此处输入图片说明

回答by leeladam

Just use the function plotas follows

只需plot按如下方式使用该功能

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

回答by Jash Shah

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

enter image description here

在此处输入图片说明