简体   繁体   English

使用matplotlib.pyplot.plot用虚线绘制图像

[英]use matplotlib.pyplot.plot plot the image with dashed line

I am learning the matplotlib. 我正在学习matplotlib。 But I can not understand the example in their official page . 但我无法理解他们官方网页上的例子。

import numpy as np
import matplotlib.pyplot as plt

x = np.linspace(0, 10)
line, = plt.plot(x, np.sin(x), '--', linewidth=2)

dashes = [10, 5, 100, 5] # 10 points on, 5 off, 100 on, 5 off
line.set_dashes(dashes)

plt.show()

The result is 结果是

result http://matplotlib.org/_images/line_demo_dash_control.png 结果http://matplotlib.org/_images/line_demo_dash_control.png

Question about the code is: 关于代码的问题是:

line, = plt.plot(x, np.sin(x), '--', linewidth=2)

What means the "," after the line? 行后的“,”是什么意思?

Thanks very much for the patient! 非常感谢病人!

A good place to start for these sorts of questions is always the documentation. 开始处理这些问题的好地方始终是文档。 Here's the docs for pyplot.plot() . 这是pyplot.plot()的文档 Note that about halfway down it says: 请注意,大约一半的时间它说:

Return value is a list of lines that were added. 返回值是已添加的行列表。

So the example uses line, instead of line in order to select just the first element in the returned list (which also happens to be the only element in this case). 因此,该示例使用line,而不是line ,以便仅选择返回列表中的第一个元素(在这种情况下,它也恰好是唯一的元素)。 You can check this out for yourself: 你可以自己检查一下:

line, = plt.plot(x, np.sin(x), '--', linewidth=2)

type(line)
Out[59]: matplotlib.lines.Line2D

So line is a Line2D object. 所以line是一个Line2D对象。 However, when we omit the comma: 但是,当我们省略逗号时:

line = plt.plot(x, np.sin(x), '--', linewidth=2)

We get: 我们得到:

type(line)
Out[61]: list

line
Out[62]: [<matplotlib.lines.Line2D at 0x7f9a04060e10>]

So in this case line is actually a list that contains one Line2D object. 所以在这种情况下, line实际上是一个包含一个Line2D对象的列表。

Here is the documentation for Line2D.set_dashes() ; 这是Line2D.set_dashes()的文档 ; see if that answers your other question. 看看是否能回答你的其他问题。

The

,

generally is to create a tuple. 一般是创建一个元组。

You can see here a detailed explanation. 你可以在这里看到详细的解释。

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

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