如何减少 matplotlib 中的填充密度
我需要降低用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是一个完整的黑客,但它应该适用于您的场景。
基本上,您可以定义一个新的填充图案,输入字符串越长,填充图案的密度就越小。我已经为您调整了
HorizontalHatch
模式(请注意下划线字符的使用):然后您必须将其添加到可用填充模式列表中:
在您的绘图代码中,您现在可以使用定义的模式:
请记住,这不是一个非常优雅的解决方案,并且可能在未来的版本中随时崩溃。另外,我的模式代码也只是一个快速破解,您可能想对其进行改进。我继承自
HorizontalHatch
,但为了获得更大的灵活性,您可以在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):You then have to add it to the list of available hatch patterns:
In your plotting code you can now use the defined pattern:
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 onHatchPatternBase
.