这是一个完整的 hack,但它应该适用于您的场景。
基本上,您可以定义一个新的填充图案,输入字符串越长,它的密度就越小。我已经为你调整了HorizontalHatch 模式(注意下划线字符的使用):
class CustomHorizontalHatch(matplotlib.hatch.HorizontalHatch):
def __init__(self, hatch, density):
char_count = hatch.count('_')
if char_count > 0:
self.num_lines = int((1.0 / char_count) * density)
else:
self.num_lines = 0
self.num_vertices = self.num_lines * 2
然后您必须将其添加到可用填充图案列表中:
matplotlib.hatch._hatch_types.append(CustomHorizontalHatch)
在您的绘图代码中,您现在可以使用定义的模式:
kwargs = {'hatch':'_'} # same as '-'
rects2 = ax.bar(theta, day7, width,fill=False, align='edge', alpha=1, **kwargs)
kwargs = {'hatch':'__'} # less dense version
rects1 = ax.bar(theta, day1, width,fill=False, align='edge', alpha=1, **kwargs)
请记住,这不是一个非常优雅的解决方案,并且可能会在未来的版本中随时中断。此外,我的模式代码也只是一个快速破解,您可能想要改进它。我继承自 HorizontalHatch,但为了获得更大的灵活性,您可以在 HatchPatternBase 上构建。