如何减少 matplotlib 中的填充密度

发布于 2024-10-12 23:24:38 字数 358 浏览 1 评论 0原文

我需要降低用 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)

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

I need to decrease the density of the hatch in a bar made with matplotlib.
The way I add the hatches:

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)

I know that you can increase the density by adding more characters to the pattern, but how can you decrease the density?!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不语却知心 2024-10-19 23:24:38

这是一个完整的黑客,但它应该适用于您的场景。

基本上,您可以定义一个新的填充图案,输入字符串越长,填充图案的密度就越小。我已经为您调整了 Horizo​​ntalHatch 模式(请注意下划线字符的使用):

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)

请记住,这不是一个非常优雅的解决方案,并且可能在未来的版本中随时崩溃。另外,我的模式代码也只是一个快速破解,您可能想对其进行改进。我继承自 Horizo​​ntalHatch,但为了获得更大的灵活性,您可以在 HatchPatternBase 上构建。

This is a complete hack, but it should work for your scenario.

Basically, you can define a new hatch pattern that becomes less dense the longer the input string. I've gone ahead and adapted the HorizontalHatch pattern for you (note the use of the underscore character):

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

You then have to add it to the list of available hatch patterns:

matplotlib.hatch._hatch_types.append(CustomHorizontalHatch)

In your plotting code you can now use the defined pattern:

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)

Bear in mind that this is not a very elegant solution and might break at any time in future versions. Also my pattern code is just a quick hack as well and you might want to improve on it. I inherit from HorizontalHatch but for more flexibility you would build on HatchPatternBase.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文