问题描述
我必须使用python(matplotlib)实现一些像这样的图片。
i have to implement some figures like that on the Picture with python (matplotlib).
有没有人知道我能做到这一点?我尝试使用多边形来创建这些变形的矩形,例如像这样:
Has anyone an idea how i could achieve this? I tried to work with polygons to create these deformed rectangulars, for example like this:
import matplotlib.pyplot as plt
plt.axes()
points = [[x, y]]
polygon = plt.Polygon(points)
plt.show()
但它只显示一个坐标系,没有别的东西,当我输入x,y点以获得变形的矩形。
but it just shows a coordinate system and nothing else when I type in the x,y points to get a deformed rectangular.
我现在使用了@ImportanceOfBeingErnest的答案,但是抛出一个错误
I now used the answer by @ImportanceOfBeingErnest, but throws an error
有没有人有一个想法来自哪里?
Does anyone have an idea where this comes from?
推荐答案
这是一种添加多边形的方法到一个matplotlib轴。多边形是 matplotlib.patches.Polygon
的一个实例,它使用 ax.add_patch
附加到轴上。
Here is a way to add a polygon to a matplotlib axes. The polygon is an instance of matplotlib.patches.Polygon
and it is appended to the axes using ax.add_patch
.
由于matplotlib不会自动缩放包含补丁的轴,因此需要设置轴限制。
Since matplotlib does not autoscale the axes to include patches, it is necessary to set the axis limits.
import matplotlib.pyplot as plt
import matplotlib.patches
x = [1,10,10,1,1]
y = [2,1,5,4,2]
points = list(zip(x,y))
polygon = matplotlib.patches.Polygon(points, facecolor="#aa0088")
fig, ax = plt.subplots()
ax.set_aspect("equal")
ax.add_patch(polygon)
ax.set_xlim(0,11)
ax.set_ylim(0,6)
plt.show()
这篇关于变形的矩形具有递减趋势的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!