假设我有一个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()
给定的示例似乎包含一些高度为零的矩形。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句