如何在Python中获取一组点的中心?

如何在Python中获取一组点的中心?

要获取一组点的中心,我们可以将列表中的所有元素相加,并将其与列表的长度相除,这样结果就是相应轴的中心。

阅读更多:Python 教程

步骤

  • 制作两个数据点列表。

  • 使用 plot() 方法绘制x和y数据点。

  • 获取x和y数据点的中心元组。

  • 将中心点放置在图上。

  • 以标签的形式注释x和y数据点的中心。

  • 使用 show() 方法显示图形。

示例

from matplotlib import pyplot as plt
plt.rcParams["figure.figsize"] = [7.00, 3.50]
plt.rcParams["figure.autolayout"] = True
x = [5, 1, 3, 2, 8]
y = [3, 6, 1, 0, 5]
plt.plot(x, y)
center = sum(x)/len(x), sum(y)/len(y)
plt.plot(center[0], center[1], marker='o')
plt.annotate(
   "center",
   xy=center, xytext=(-20, 20),
   textcoords='offset points', ha='right', va='bottom',
   bbox=dict(boxstyle='round,pad=0.5', fc='yellow', alpha=0.5),
   arrowprops=dict(arrowstyle='->', connectionstyle='arc3,rad=0'))
plt.show()
Python

输出

如何在Python中获取一组点的中心?

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程

登录

注册