【问题标题】:How to decrease hatch density in matplotlib如何降低 matplotlib 中的孵化密度
【发布时间】:2011-01-20 10:27:14
【问题描述】:

我需要在使用 matplotlib 制作的条中降低孵化的密度。 我添加阴影的方式:

kwargs = {'hatch':'|'}
rects2 = ax.bar(theta, day7, width,fill=False, align='edge', alpha=1, **kwargs)

kwargs = {'hatch':'-'}
rects1 = ax.bar(theta, day1, width,fill=False, align='edge', alpha=1, **kwargs)

我知道可以通过向图案中添加更多字符来增加密度,但是如何减少密度?!

【问题讨论】:

  • 可以在孵化中添加空格吗?

标签: python matplotlib


【解决方案1】:

这是一个完整的 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 上构建。

【讨论】:

    猜你喜欢
    • 2012-10-14
    • 2022-07-06
    • 2016-07-11
    • 2016-12-21
    • 1970-01-01
    • 1970-01-01
    • 2017-11-19
    • 1970-01-01
    • 2015-12-13
    相关资源
    最近更新 更多