簡體   English   中英

Plot 數字而不是使用 Matplotlib 散點圖的點

[英]Plot Numbers Instead of Points Using Matplotlib Scatterplot

我正在嘗試 plot 散點圖,但我想要 plot 數字而不是點或三角形或其他符號。 例如,有以下幾點:

centroidCoords = [Point(7.210123936676805, -0.0014481952154823),
                  Point(5.817327756517152, -1.0513260084561042),
                  Point(5.603133733696165, -2.7765635631249412),
                  Point(4.500525247710033, -0.8659667639805515),
                  Point(3.9999999999880367, -2.089987631283091),

我可以在像這樣的散點圖中 plot :

# xs = [point.x for point in centroidCoords]
# ys = [point.y for point in centroidCoords]
# plt.scatter(xs, ys)

但不是小圓點,我想知道我是否可以 plot 數字。 第一個點可以是 1,第二個點可以是 2,等等。我嘗試使用 zip 來分配數字,如下所示:

num_label = range(0,len(centroidCoords),1)
numberedCentroids = zip(centroidCoords, num_label)
print(numberedCentroids)

但它沒有像我想象的那樣打印或 plot 。 我想要的只是一個 plot,數字 1 在點 1 坐標,數字 2 在點 2 坐標,等等。最終我將在后面添加多邊形,這將看起來像那些“為數字着色”的東西之一。

我對 python 還是很陌生,因此將不勝感激任何幫助!

Matplotlib 的文檔說plt.scatter有一個名為“marker”的參數,它定義了在繪圖上用作“點”的內容,但對於使用相同命令繪制的所有數據,它將是相同的。 所以我想如果你想為每個坐標對改變它,你可以這樣做:

for i in range(len(centroidCoords)):
    plt.scatter(centroidCoords[i].x,centroidCoords[i].y, marker="$"+str(i)+"$")

美元符號是必要的,因此它會將數字呈現為常規文本。

您可以在任何地方只使用 plot 文本,但您必須通過調用.xlim().ylim()手動設置圖像的限制

import numpy as np
import matplotlib.pyplot as plt

centroidCoords = [(7.210123936676805, -0.0014481952154823),
                  (5.817327756517152, -1.0513260084561042),
                  (5.603133733696165, -2.7765635631249412),
                  (4.500525247710033, -0.8659667639805515),
                  (3.9999999999880367, -2.089987631283091)]

textCoord = [f"{x:.2f} {y:.2f}" for x, y in centroidCoords]
x, y = np.array(centroidCoords).T

for col, row, t in zip(x, y, textCoord):
    plt.text(col, row, t, fontsize=10, color="red")
plt.xlim(min(x)-1, max(x)+1)
plt.ylim(min(y)-1, max(y)+1)
plt.show()

在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM