如何删除 Matplotlib 绘图中的线条?

matplotlibserver side programmingprogramming

我们将创建两条线条,即 line1 和 line2。之后,我们将弹出第二行并将其删除。

步骤

  • 为 line1 和 line2 创建列表。

  • 使用 plot() 方法绘制 line1 和 line 2,其中 line 2 的样式为"dashed"。

  • 设置或获取自动缩放边距 (0.2)。

  • 弹出 line 2,并使用 remove() 方法将其删除。

  • 最终图形只有一行,因此请使用 plt.show() 方法。

示例

import matplotlib.pyplot as plt

line1 = [2, 4, 8]
line2 = [3, 6, 12]

plt.plot(line1)
line_2 = plt.plot(line2, linestyle='dashed')
plt.margins(0.2)
plt.title("包含多余的线条")
plt.show()

plt.plot(line1)
l = line_2.pop(0)
l.remove()
plt.margins(0.2)
plt.title("删除多余的线条")

plt.show()

输入图(删除线条前)

输出图表


相关文章