gpt4 book ai didi

python - 图中图

转载 作者:太空宇宙 更新时间:2023-11-03 11:49:20 26 4
gpt4 key购买 nike

我正在尝试使用 matplotlib 在数字中绘制数字。由于正方形是最直接绘制的,所以我从这些开始。最后我想为具有一定宽度的多边形编写一个生成器。在给定的示例中,这将是一个具有直角和宽度 1 的 4 角多边形。

我当前的代码绘制了以下内容,这符合预期并且几乎符合要求。

square in square in square

请注意,2,22,3 之间有一行,我认为如果使用正确的算法而不是当前代码来完成,则可以将其删除。

上面的总结是一个由两个框组成的方框,幅度随 1 增加,假设较大的框“位于”其余框的“后面”。

我编写生成上述代码的方法实际上并不是一个函数。这是一个非常丑陋的点集合,恰好类似于空心正方形。

import matplotlib.path as mpath
import matplotlib.patches as mpatches
import matplotlib.pyplot as plt

fig, ax = plt.subplots()

INNER_AMPLITUDE = 1.0
OUTER_AMPLITUDE = 3.0

Path_in = mpath.Path
path_in_data = [
(Path_in.MOVETO, (INNER_AMPLITUDE, -INNER_AMPLITUDE)),
(Path_in.LINETO, (-INNER_AMPLITUDE, -INNER_AMPLITUDE)),
(Path_in.LINETO, (-INNER_AMPLITUDE, INNER_AMPLITUDE)),
(Path_in.LINETO, (INNER_AMPLITUDE, INNER_AMPLITUDE)),
(Path_in.CLOSEPOLY, (INNER_AMPLITUDE, -INNER_AMPLITUDE)),
]
codes, verts = zip(*path_in_data)
path_in = mpath.Path(verts, codes)
patch_in = mpatches.PathPatch(path_in, facecolor='g', alpha=0.3)
ax.add_patch(patch_in)

x, y = zip(*path_in.vertices)
line, = ax.plot(x, y, 'go-')

Path_out = mpath.Path
path_out_data = [
(Path_out.MOVETO, (OUTER_AMPLITUDE, -OUTER_AMPLITUDE)),
(Path_out.LINETO, (-OUTER_AMPLITUDE, -OUTER_AMPLITUDE)),
(Path_out.LINETO, (-OUTER_AMPLITUDE, OUTER_AMPLITUDE)),
(Path_out.LINETO, (OUTER_AMPLITUDE, OUTER_AMPLITUDE)),
(Path_out.LINETO, (OUTER_AMPLITUDE, OUTER_AMPLITUDE-INNER_AMPLITUDE)),
(Path_out.LINETO, (-(OUTER_AMPLITUDE-INNER_AMPLITUDE), OUTER_AMPLITUDE-INNER_AMPLITUDE)),
(Path_out.LINETO, (-(OUTER_AMPLITUDE-INNER_AMPLITUDE), -(OUTER_AMPLITUDE-INNER_AMPLITUDE))),
(Path_out.LINETO, (OUTER_AMPLITUDE-INNER_AMPLITUDE, -(OUTER_AMPLITUDE-INNER_AMPLITUDE))),
(Path_out.LINETO, (OUTER_AMPLITUDE-INNER_AMPLITUDE, OUTER_AMPLITUDE-INNER_AMPLITUDE)),
(Path_out.LINETO, (OUTER_AMPLITUDE, OUTER_AMPLITUDE-INNER_AMPLITUDE)),
(Path_out.CLOSEPOLY, (OUTER_AMPLITUDE, OUTER_AMPLITUDE-INNER_AMPLITUDE)),
]
codes, verts = zip(*path_out_data)
path_out = mpath.Path(verts, codes)
patch_out = mpatches.PathPatch(path_out, facecolor='r', alpha=0.3)
ax.add_patch(patch_out)
plt.title('Square in a square in a square')

ax.grid()
ax.axis('equal')
plt.show()

请注意,我认为这与代码审查无关,因为我正在寻求扩展我的功能,而不仅仅是符合最佳实践的重写。我觉得我的做法完全错误。要事第一。

我应该如何使用 matplotlib 绘制具有一定宽度的多边形,假设多边形将在外部被相同形式且至少相同宽度的带包围并且在内部完全填充?

最佳答案

纯粹在 matplotlib 中处理多边形可能非常乏味。幸运的是,有一个非常好的库用于此类操作:shapely .对于您的目的,parallel_offset 函数是可行的方法。您感兴趣的多边形边界由 ring1ring2ring3 定义:

import numpy as np
import matplotlib.pyplot as plt
import shapely.geometry as sg
from descartes.patch import PolygonPatch

# if I understood correctly you mainly need the difference d here
INNER_AMPLITUDE = 0.1
OUTER_AMPLITUDE = 0.2
d = OUTER_AMPLITUDE - INNER_AMPLITUDE

# fix seed, for reproducability
np.random.seed(11111)

# a function to produce a "random" polygon
def random_polygon():
nr_p = np.random.randint(7,15)
angle = np.sort(np.random.rand(nr_p)*2*np.pi)
dist = 0.3*np.random.rand(nr_p) + 0.5
return np.vstack((np.cos(angle)*dist, np.sin(angle)*dist)).T

# your input polygon
p = random_polygon()

# create a shapely ring object
ring1 = sg.LinearRing(p)
ring2 = ring1.parallel_offset(d, 'right', join_style=2, mitre_limit=10.)
ring3 = ring1.parallel_offset(2*d, 'right', join_style=2, mitre_limit=10.)

# revert the third ring. This is necessary to use it to procude a hole
ring3.coords = list(ring3.coords)[::-1]

# inner and outer polygon
inner_poly = sg.Polygon(ring1)
outer_poly = sg.Polygon(ring2, [ring3])

# create the figure
fig, ax = plt.subplots(1)

# convert them to matplotlib patches and add them to the axes
ax.add_patch(PolygonPatch(inner_poly, facecolor=(0,1,0,0.4),
edgecolor=(0,1,0,1), linewidth=3))
ax.add_patch(PolygonPatch(outer_poly, facecolor=(1,0,0,0.4),
edgecolor=(1,0,0,1), linewidth=3))

# cosmetics
ax.set_aspect(1)
plt.axis([-1.5, 1.5, -1.5, 1.5])
plt.grid()
plt.show()

结果:

enter image description here

关于python - 图中图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31865990/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com