如何在Matplotlib中创建两个点之间的线段?

如何在Matplotlib中创建两个点之间的线段?

要在matplotlib中创建两个点之间的线段,可以按照以下步骤进行操作

  • 设置图形大小并调整子图之间和周围的填充。
  • 为了创建两个点,请创建两个列表。
  • point1point2 中提取 xy 值。
  • 使用 plot() 方法绘制 xy 值的线段。
  • 为两个点放置文本。
  • 使用 show() 方法来显示图形。

实例

import matplotlib.pyplot as plt
plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True
point1 = [1, 2]
point2 = [3, 4]
x_values = [point1[0], point2[0]]
y_values = [point1[1], point2[1]]
plt.plot(x_values, y_values, 'bo', linestyle="--")
plt.text(point1[0]-0.015, point1[1]+0.25, "Point1")
plt.text(point2[0]-0.050, point2[1]-0.25, "Point2")
plt.show()

输出

如何在Matplotlib中创建两个点之间的线段?

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程