如何更改 matplotlib 在补丁中平铺自定义阴影线的方式?我想以网格图案(就像方形瓷砖)平铺舱口,而不是交替行(就像砌砖)。
┌───┐┌───┐┌───┐
│ ││ ││ │
└───┘└───┘└───┘
┌───┐┌───┐┌───┐
│ ││ ││ │
└───┘└───┘└───┘
对
┌───┐┌───┐┌───┐
│ ││ ││ │
└───┘└───┘└───┘
──┐┌───┐┌───┐┌─
││ ││ ││
──┘└───┘└───┘└─
这是在 matplotlib 中生成简单的自定义填充的示例代码:
import matplotlib.pyplot as plt
import matplotlib.patches as patches
import matplotlib.hatch
square_path = patches.Polygon(
[[-0.4, -0.4], [4.0, -0.4], [0.4, 0.4], [-0.4, 0.4]],
closed=True, fill=False).get_path()
class SquareHatch(matplotlib.hatch.Shapes):
"""Custom hatches defined by a path drawn inside [-0.5, 0.5] square.
Identifier 's'.
"""
filled = True
size = 1.0
path = square_path
def __init__(self, hatch, density):
"""Initialize the custom hatch."""
self.num_rows = int((hatch.count('s')) * density)
self.shape_vertices = self.path.vertices
self.shape_codes = self.path.codes
matplotlib.hatch.Shapes.__init__(self, hatch, density)
matplotlib.hatch._hatch_types.append(SquareHatch)
# Create a figure and axis
self.fig, self.ax = plt.subplots()
# Create a square patch with hatching
square = patches.Rectangle(xy=(0.25, 0.25), width=0.5, height=0.5, hatch='s', fill=False)
# Add the square patch to the axis
self.ax.add_patch(square)
# Display the plot
plt.show()
我一直在寻找,我认为这是一个选择,但我没有找到。 AI 说这是可能的,但给了我一个不起作用的选项......我会祭坛我的瓷砖,但我无法找到一种方法让我的图案与偏移第二行一起工作。
问题来自
Shapes 超类中的
set_vertices_and_codes
函数:
def set_vertices_and_codes(self, vertices, codes):
offset = 1.0 / self.num_rows
shape_vertices = self.shape_vertices * offset * self.size
shape_codes = self.shape_codes
if not self.filled:
shape_vertices = np.concatenate( # Forward, then backward.
[shape_vertices, shape_vertices[::-1] * 0.9])
shape_codes = np.concatenate([shape_codes, shape_codes])
vertices_parts = []
codes_parts = []
for row in range(self.num_rows + 1):
####### Offsetting occurs here #############
if row % 2 == 0:
cols = np.linspace(0, 1, self.num_rows + 1)
else:
cols = np.linspace(offset / 2, 1 - offset / 2, self.num_rows)
####### Offsetting occurs here #############
row_pos = row * offset
for col_pos in cols:
vertices_parts.append(shape_vertices + [col_pos, row_pos])
codes_parts.append(shape_codes)
np.concatenate(vertices_parts, out=vertices)
np.concatenate(codes_parts, out=codes)
无论行号如何,将列更改为
np.linspace(0, 1, self.num_rows+1)
都会删除偏移量,但现在之前在 Shape 类的 self.num_shapes
中设置的 __init__
已关闭。最终,我发现自定义填充更容易复制粘贴 Shape 类,并进行一些小的编辑来删除偏移。
class SquareHatch(matplotlib.hatch.HatchPatternBase):
"""Custom hatches defined by a path drawn inside [-0.5, 0.5] square.
Identifier 's'.
"""
filled = True
size = 1.0
path = square_path
def __init__(self, hatch, density):
"""Initialize the custom hatch."""
self.num_rows = int((hatch.count('s')) * density)
self.shape_vertices = self.path.vertices
self.shape_codes = self.path.codes
self.num_shapes = (self.num_rows+1)**2
self.num_vertices = (self.num_shapes *
len(self.shape_vertices) *
(1 if self.filled else 2))
def set_vertices_and_codes(self, vertices, codes):
offset = 1.0 / self.num_rows
shape_vertices = self.shape_vertices * offset * self.size
shape_codes = self.shape_codes
if not self.filled:
shape_vertices = np.concatenate( # Forward, then backward.
[shape_vertices, shape_vertices[::-1] * 0.9])
shape_codes = np.concatenate([shape_codes, shape_codes])
vertices_parts = []
codes_parts = []
for row in range(self.num_rows + 1):
cols = np.linspace(0, 1, self.num_rows + 1)
row_pos = row * offset
for col_pos in cols:
vertices_parts.append(shape_vertices + [col_pos, row_pos])
codes_parts.append(shape_codes)
np.concatenate(vertices_parts, out=vertices)
np.concatenate(codes_parts, out=codes)