【发布时间】:2018-01-29 19:40:48
【问题描述】:
我想用线填充填充一堆多边形。线条必须相对于 x 轴具有特定角度。我发现 matplotlib 已经支持一些孵化类,并且可以定义一个自定义类(如How to fill a polygon with a custom hatch in matplotlib?)。我试图生成一个自定义舱口,但是当我将它附加到舱口列表时,init 函数不知道角度。我尝试了以下课程:
class AngularHatch(HatchPatternBase):
def __init__(self, hatch, density, angle):
self.num_lines = int((hatch.count('{'))*density*3)
self.num_vertices = self.num_lines * 2
self.R = np.array([[np.cos(angle), -np.sin(angle)],
[np.sin(angle), np.cos(angle)]])
def set_vertices_and_codes(self, vertices, codes):
steps, stepsize = np.linspace(0.0, 1.0, self.num_lines, False,
retstep=True)
steps += stepsize / 2.
vertices[0::2, 0] = 0
vertices[0::2, 1] = steps
vertices[1::2, 0] = 1
vertices[1::2, 1] = steps
for i, v in enumerate(vertices):
vertices[i] = self.R.dot(v)
codes[0::2] = Path.MOVETO
codes[1::2] = Path.LINETO
然后我将这个类添加到孵化的可用类列表中。但是,这不会生成正确的行,因为代码是从 HorizontalHatch 源代码here 修改的,我认为这会在单位正方形中生成行。此外,我需要为要渲染的每个多边形生成一个特定角度的补丁。 ¿ 有关如何为每个多边形的此类提供正确角度的任何想法?
【问题讨论】:
标签: python python-3.x numpy matplotlib polygon