图中的图中的图

时间:2015-08-06 21:19:17

标签: python python-2.7 matplotlib

我试验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()

请注意我认为这是Code Review的主题,因为我正在寻求扩展我的功能,而不仅仅是重写最佳实践。我觉得我完全以错误的方式做这件事。首先要做的事情。

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

1 个答案:

答案 0 :(得分:1)

纯粹在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