matplotlib 2d line line,=plot comma meaning

荒凉一梦 提交于 2019-11-27 22:23:25

plt.plot returns a list of the Line2D objects plotted, even if you plot only one line.

That comma is unpacking the single value into line.

ex

a, b = [1, 2]
a, = [1, ]

The plot method returns objects that contain information about each line in the plot as a list. In python, you can expand the elements of a list with a comma. For example, if you plotted two lines, you would do:

line1, line2 = plt.plot(x,y,'-',x,z,':')

Where line1 would correspond to x,y, and line2 corresponds to x,z. In your example, there is only one line, so you need the comma to tell it to expand a 1-element list. What you have is equivalent to

line = plt.plot(x,y,'-')[0]

or

line = ply.plot(x,y,'-')
line = line[0]

Your code should work if you omit the comma, only because you are not using line. In your simple example plt.plot(x,y,'-') would be enough.

The return value of the function is a tuple or list containing one item, and this syntax "unpacks" the value out of the tuple/list into a simple variable.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!