简体   繁体   English

matplotlib.pyplot.plot() 如何连接散点?

[英]How does matplotlib.pyplot.plot() connect scatter points?

When we use pyplot.plot(x, y) in matplotlib, a line plot, which connects all the points (vertices), is drawn by default.当我们在 matplotlib 中使用pyplot.plot(x, y)时,默认绘制一条连接所有点(顶点)的线 plot。 I want to understand, exactly how does plot() do this?我想了解, plot()究竟是如何做到的? How does it connect the points?它如何连接点? Does it use something like Bresenham's Line algorithm to accomplish this?它是否使用类似 Bresenham 的 Line 算法来实现这一点?

Matplotlib doesn't do anything more than connect the points in the order they were given Matplotlib 除了按照给定的顺序连接点外,什么也不做

You pass two arrays with x and y coordinates.您传递了两个具有 x 和 y 坐标的 arrays 。 Matplotlib takes the first point (x1,y1) and draws a line to the second pair (x2,y2), then from the second pair to the third and so on until the last pair Matplotlib 取第一个点 (x1,y1) 并画一条线到第二对 (x2,y2),然后从第二对到第三对,依此类推,直到最后一对

x = [1,2,3]
y = [1,5,2]
plt.plot(x,y, 'ro-')

在此处输入图像描述

If I plot two points, I get a list containing one Line object如果我plot两点,我得到一个包含Line object 的列表

In [58]: g=plt.gca()                                                                           
In [59]: g                                                                                     
Out[59]: <matplotlib.axes._subplots.AxesSubplot at 0x7fce94a26400>
In [60]: g.plot([0,1],[1,2])                                                                   
Out[60]: [<matplotlib.lines.Line2D at 0x7fce94a26160>]

https://matplotlib.org/api/_as_gen/matplotlib.lines.Line2D.html#matplotlib.lines.Line2D https://matplotlib.org/api/_as_gen/matplotlib.lines.Line2D.html#matplotlib.lines.Line2D

That in turn has many attributes and methods, though a particularly relevant one appears to be a Path object这又具有许多属性和方法,尽管一个特别相关的似乎是Path object

In [67]: aline._path                                                                           
Out[67]: 
Path(array([[0., 1.],
       [1., 2.]]), None)
In [68]: aline._path.__dict__                                                                  
Out[68]: 
{'_vertices': array([[0., 1.],
        [1., 2.]]),
 '_codes': None,
 '_interpolation_steps': 1,
 '_simplify_threshold': 0.1111111111111111,
 '_should_simplify': False,
 '_has_nonfinite': False,
 '_readonly': False}

https://matplotlib.org/api/path_api.html#matplotlib.path.Path https://matplotlib.org/api/path_api.html#matplotlib.path.Path

Note its code types includes pen drawing directions like: move to and line to .请注意,它的代码类型包括画笔方向,例如: move toline to Graphics backends often take commands like: "put the pen at x", "draw line to y", "lift pen".图形后端通常采用以下命令:“将笔放在 x”、“将线画到 y”、“抬起笔”。 The backend takes care of interpolating the points between x and y, not the commanding code.后端负责插入 x 和 y 之间的点,而不是命令代码。 In that case the edge is not accessible to you.在这种情况下,您无法访问edge

While it doesn't use matplotlib , you might find the turtle graphics package instructive.虽然它不使用matplotlib ,但您可能会发现turtle图形 package 具有指导意义。 That lets you command a drawing pen directly.这使您可以直接控制绘图pen

https://docs.python.org/3/library/turtle.html https://docs.python.org/3/library/turtle.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM