簡體   English   中英

如何繪制在 Python 中表示為元組的行列表?

[英]How do I plot list of lines represented as tuples in Python?

假設,我有一個 Python 函數,它從名為marching_square()的方法返回檢測到的行。

說,我有以下數據集。 我想使用 MatPlotLib 來繪制數據。

元組的形式為(x1, y1, x2, y2)

 [(20.0, 10, 20, 10.0), (20, 10.0, 30, 10.0),
(30, 10.0, 40, 10.0), (40.0, 10, 40, 10.0),
(70.0, 10, 70, 10.0), (70, 10.0, 80, 10.0),
(80, 10.0, 90, 10.0), (90.0, 10, 90, 10.0),
(10.0, 20, 10, 20.0), (10.0, 20, 20, 10.0),
(20.0, 10, 10, 20.0), (20, 10.0, 30, 10.0),
(30, 10.0, 40, 10.0), (50.0, 20, 40, 10.0),
(40.0, 10, 50, 20.0), (50, 20.0, 60, 20.0),
(60.0, 20, 70, 10.0), (70.0, 10, 60, 20.0),
(70, 10.0, 80, 10.0), (80, 10.0, 90, 10.0),
(100.0, 20, 90, 10.0), (90.0, 10, 100, 20.0),
(10.0, 20, 10, 20.0), (20.0, 30, 10, 20.0),
(10.0, 20, 20, 30.0), (20.0, 30, 20, 30.0),
(40.0, 30, 40, 30.0), (40.0, 30, 50, 20.0),
(50.0, 20, 40, 30.0), (50, 20.0, 60, 20.0),
(70.0, 30, 60, 20.0), (60.0, 20, 70, 30.0),
(70.0, 30, 70, 30.0), (100.0, 30, 100.0, 20),
(20.0, 30, 20, 30.0), (30.0, 40, 20, 30.0),
(20.0, 30, 30, 40.0), (30.0, 40, 40, 30.0),
(40.0, 30, 30, 40.0), (40.0, 30, 40, 30.0),
(70.0, 30, 70, 30.0), (80.0, 40, 70, 30.0),
(70.0, 30, 80, 40.0), (80, 40.0, 90, 40.0),
(90.0, 40, 100, 30.0), (100.0, 30, 90, 40.0)]

如何才能做到這一點?


example = np.array([
                    [ 0,0, 0, 0, 0, 0,0,0,0,0,0],
                    [ 0,0, 1, 1, 1, 0,0,1,1,1,0 ],
                    [ 0,1, 0, 0, 0, 1,1,0,0,0,1 ],
                    [ 0,0, 1, 0, 1, 0,0,1,0,0,1 ],
                    [ 0,0, 0, 1, 0, 0,0,0,1,1,0 ]
                ]);

x = [0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100];
y = [0, 10, 20, 30, 40];

collection = marching_square(x, y, example, 1);

for ln in collection:
    for toup in ln:
        line = list(toup)
        plt.plot(line)
        
plt.show()

在此處輸入圖片說明

您可以提取每個元組的x1, y1, x2, y2 ,然后創建矩形邊緣和填充:

import matplotlib.pyplot as plt

collection = [(20, 10, 20, 10), (20, 10, 30, 10), (30, 10, 40, 10), (40, 10, 40, 10), (70, 10, 70, 10), (70, 10, 80, 10), (80, 10, 90, 10), (90, 10, 90, 10), (10, 20, 10, 20), (10, 20, 20, 10), (20, 10, 10, 20), (20, 10, 30, 10), (30, 10, 40, 10), (50, 20, 40, 10), (40, 10, 50, 20), (50, 20, 60, 20), (60, 20, 70, 10), (70, 10, 60, 20), (70, 10, 80, 10), (80, 10, 90, 10), (100, 20, 90, 10), (90, 10, 100, 20), (10, 20, 10, 20), (20, 30, 10, 20), (10, 20, 20, 30), (20, 30, 20, 30), (40, 30, 40, 30), (40, 30, 50, 20), (50, 20, 40, 30), (50, 20, 60, 20), (70, 30, 60, 20), (60, 20, 70, 30), (70, 30, 70, 30), (100, 30, 100, 20), (20, 30, 20, 30), (30, 40, 20, 30), (20, 30, 30, 40), (30, 40, 40, 30), (40, 30, 30, 40), (40, 30, 40, 30), (70, 30, 70, 30), (80, 40, 70, 30), (70, 30, 80, 40), (80, 40, 90, 40), (90, 40, 100, 30), (100, 30, 90, 40)]

for x1, y1, x2, y2 in collection:
    plt.plot([x1, x1, x2, x2, x1], [y1, y2, y2, y1, y1])
    plt.fill([x1, x1, x2, x2, x1], [y1, y2, y2, y1, y1], alpha=0.3)
plt.show()

長方形

給定的示例似乎包含一些高度為零的矩形。

暫無
暫無

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

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