Python&;Matplot:如何通过点绘制简单形状?
我只想通过点绘制简单的形状,如下所示:Python&;Matplot:如何通过点绘制简单形状?,python,matplotlib,Python,Matplotlib,我只想通过点绘制简单的形状,如下所示: import matplotlib.pyplot as plt rectangle = [(0,0),(0,1),(1,1),(1,0)] hexagon = [(0,0),(0,1),(1,2),(2,1),(2,0),(1,-1)] l_shape = [(0,0),(0,3),(1,3),(1,1),(3,1),(3,0)] concave = [(0,0),(0,3),(1,3),(1,1),(2,1),(2,3),(3,3),(3,0)] f
import matplotlib.pyplot as plt
rectangle = [(0,0),(0,1),(1,1),(1,0)]
hexagon = [(0,0),(0,1),(1,2),(2,1),(2,0),(1,-1)]
l_shape = [(0,0),(0,3),(1,3),(1,1),(3,1),(3,0)]
concave = [(0,0),(0,3),(1,3),(1,1),(2,1),(2,3),(3,3),(3,0)]
for points in [rectangle, hexagon, l_shape, concave]:
# 1. Can I get rid of the zip? plot directly by points
# 2. How can I make the shape complete?
xs, ys = zip(*points)
plt.plot(xs, ys, 'o')
plt.plot(xs, ys, '-')
automin, automax = plt.xlim()
plt.xlim(automin-0.5, automax+0.5)
automin, automax = plt.ylim()
plt.ylim(automin-0.5, automax+0.5)
# Can I display the shapes 2 in 1 line?
plt.show()
rectangleX = [0, 0, 1, 1, 0]
rectangleY = [0, 1, 1, 0, 0]
plt.plot(rectangleX, rectangleY, 'o')
plt.plot(rectangleX, rectangleY, '-')
我的问题是
*zip
?我的意思是,直接按点绘制,而不是2个数组完整
?既然我在所有的点上循环,第一个点和最后一个点不能连接在一起,我怎么能做到呢凸面外壳?)
要关闭形状,只需在列表末尾再次添加第一个点:
# rectangle = [(0,0),(0,1),(1,1),(1,0)]
rectangle = [(0,0),(0,1),(1,1),(1,0),(0,0)]
plt.plot
获取x坐标列表和y坐标列表。我想说的是,你现在做这件事的方式已经是“按点而不是按2个数组”来做了。因为如果您想在不使用zip
的情况下进行此操作,它将如下所示:
import matplotlib.pyplot as plt
rectangle = [(0,0),(0,1),(1,1),(1,0)]
hexagon = [(0,0),(0,1),(1,2),(2,1),(2,0),(1,-1)]
l_shape = [(0,0),(0,3),(1,3),(1,1),(3,1),(3,0)]
concave = [(0,0),(0,3),(1,3),(1,1),(2,1),(2,3),(3,3),(3,0)]
for points in [rectangle, hexagon, l_shape, concave]:
# 1. Can I get rid of the zip? plot directly by points
# 2. How can I make the shape complete?
xs, ys = zip(*points)
plt.plot(xs, ys, 'o')
plt.plot(xs, ys, '-')
automin, automax = plt.xlim()
plt.xlim(automin-0.5, automax+0.5)
automin, automax = plt.ylim()
plt.ylim(automin-0.5, automax+0.5)
# Can I display the shapes 2 in 1 line?
plt.show()
rectangleX = [0, 0, 1, 1, 0]
rectangleY = [0, 1, 1, 0, 0]
plt.plot(rectangleX, rectangleY, 'o')
plt.plot(rectangleX, rectangleY, '-')
更新:
要获得更好的多边形支持,请使用模块[]。这可能更符合您所寻找的内容。默认情况下(closed=True
),它将为您关闭路径,还允许您将顶点直接添加到列表(而不是两个单独的列表):
下面的代码不使用临时变量
xs
和ys
,而是直接解包元组。我还从点列表中添加第一个点,以使形状完整
rectangle = [(0,0),(0,1),(1,1),(1,0)]
hexagon = [(0,0),(0,1),(1,2),(2,1),(2,0),(1,-1)]
l_shape = [(0,0),(0,3),(1,3),(1,1),(3,1),(3,0)]
concave = [(0,0),(0,3),(1,3),(1,1),(2,1),(2,3),(3,3),(3,0)]
for points in [rectangle, hexagon, l_shape, concave]:
plt.plot(*zip(*(points+points[:1])), marker='o')
automin, automax = plt.xlim()
plt.xlim(automin-0.5, automax+0.5)
automin, automax = plt.ylim()
plt.ylim(automin-0.5, automax+0.5)
plt.show()
在LeekaiInethsky的帖子中提供这个答案作为替代,但我觉得再次添加第一点是相当重复的,有没有更优雅的方式来做这件事?这需要按特定的顺序添加点吗?因此,它可以显示为一个形状,并且形状上不会有线条?边缘将按照提供的顺序绘制,与您之前所做的相同。明白了,很遗憾,它无法解决我的#3问题。我认为这是一个相当大的问题。我会把它编辑成一个独立的问题。试试这个或这个。你为什么要去掉*zip
?这是一种绘制点并用线连接点的方便方法。@Alik我的意思是这样的plt.plot([(0,0)、(0,1)、(1,1)、(1,0)],'o')
,因此我可以直接将点传递到plot
,而无需压缩它们。然后执行plt.plot(*zip(*(点+点[:1])),标记,而不是xs ys=zip(*点)plt.plot(xs,ys,'o')plt.plot(xs,ys,'-')
这将解决#1和#2。至于#3-我没有得到你真正想要的。l_形的凸面外壳将不同,它不包含(1,1)
point@Alik谢谢至于第三,我只是想描述一下我想要什么。当然convxl hull
就足够了。我认为更恰当的描述应该是在不排序点的情况下绘制简单形状
?