[英]how to remove line connecting discontinued point in matplotlib.pyplot.plot
[英]use matplotlib.pyplot.plot plot the image with dashed line
我正在學習matplotlib。 但我無法理解他們官方網頁上的例子。
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()
結果是
結果http://matplotlib.org/_images/line_demo_dash_control.png
關於代碼的問題是:
line, = plt.plot(x, np.sin(x), '--', linewidth=2)
行后的“,”是什么意思?
非常感謝病人!
開始處理這些問題的好地方始終是文檔。 這是pyplot.plot()的文檔 。 請注意,大約一半的時間它說:
返回值是已添加的行列表。
因此,該示例使用line,
而不是line
,以便僅選擇返回列表中的第一個元素(在這種情況下,它也恰好是唯一的元素)。 你可以自己檢查一下:
line, = plt.plot(x, np.sin(x), '--', linewidth=2)
type(line)
Out[59]: matplotlib.lines.Line2D
所以line
是一個Line2D對象。 但是,當我們省略逗號時:
line = plt.plot(x, np.sin(x), '--', linewidth=2)
我們得到:
type(line)
Out[61]: list
line
Out[62]: [<matplotlib.lines.Line2D at 0x7f9a04060e10>]
所以在這種情況下, line
實際上是一個包含一個Line2D對象的列表。
這是Line2D.set_dashes()的文檔 ; 看看是否能回答你的其他問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.