Python 使用 matplotlib 同时绘制两个函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16351436/
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
Plotting two functions simultaneously with matplotlib
提问by MITjanitor
basically I want to graph two functions
基本上我想绘制两个函数
g1 = x*cos(x*pi)
g2 = 1 - 0.6x^2
and then plot the intersection, I already have a module that takes inputs close to the two lines intersections, and then converges to those points (there's four of them)
然后绘制交点,我已经有一个模块,该模块将输入靠近两条线的交点,然后收敛到这些点(其中有四个)
but I want to graph these two functions and their intersections using matplotlib but have no clue how. I've only graphed basic functions. Any help is greatly appreciated
但我想使用 matplotlib 绘制这两个函数及其交集的图形,但不知道如何绘制。我只绘制了基本功能。任何帮助是极大的赞赏
采纳答案by DarenW
Assuming you can get as far as plotting one function, with x and g1 as numpy arrays,
假设您可以绘制一个函数,将 x 和 g1 作为 numpy 数组,
pylab.plot(x,g1)
just call plot again (and again) to draw any number of separate curves:
只需再次(一次又一次)调用 plot 即可绘制任意数量的单独曲线:
pylab.plot(x,g2)
finally display or save to a file:
最后显示或保存到文件:
pylab.show()
To indicate a special point such as an intersection, just pass in scalars for x, y and ask for a marker such 'x' or 'o' or whatever else you like.
要指示一个特殊的点,例如一个交叉点,只需传入 x、y 的标量并要求一个标记,例如“x”或“o”或其他任何你喜欢的标记。
pylab.plot(x_intersect, y_intersect, 'x', color="#80C0FF")
Alternatively, I often mark a special place along x with a vertical segment by plotting a quick little two-point data set:
或者,我经常通过绘制一个快速的小两点数据集来标记一个带有垂直线段的沿 x 的特殊位置:
pylab.plot( [x_special, x_special], [0.5, 1.9], '-b' )
I may hardcode the y values to look good on a plot for my current project, but obviously this is not reusable for other projects. Note that plot() can take ordinary python lists; no need to convert to numpy arrays.
我可能会硬编码 y 值,以便在我当前项目的绘图上看起来不错,但显然这不能用于其他项目。注意 plot() 可以使用普通的 python 列表;无需转换为 numpy 数组。
If you can't get as far as plotting one function (just g1) then you need a basic tutorial in matplot lib, which wouldn't make a good answer here but please go visit http://matplotlib.org/and google "matplotlib tutorial" or "matplotlib introduction".
如果您无法绘制一个函数(仅 g1),那么您需要一个 matplot lib 中的基本教程,这在这里不能很好地回答,但请访问http://matplotlib.org/和谷歌“ matplotlib 教程”或“matplotlib 介绍”。

