如何使用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()

輸出

更新於:2021 年 5 月 12 日

5K+ 瀏覽次數

提升您的職業生涯

完成課程即可獲得認證

開始
廣告
© . All rights reserved.