如何为条形和楔形添加纹理? [英] How can I add textures to my bars and wedges?

查看:81
本文介绍了如何为条形和楔形添加纹理?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 matplotlib.pyplot.bar() matplotlib.pyplot.pie() .在这两个功能中,我都可以更改条形和楔形的颜色.

I'm drawing several bar and pie charts using matplotlib.pyplot.bar() and matplotlib.pyplot.pie(). In both functions, I can change the colors of the bars and wedges.

但是,我需要以黑白方式打印这些图表.能够在条形和楔形上放置纹理会更有用,类似于 Line2D 标记属性,可用于绘制线条.我可以用一致的方式用这些标记填充条形图和楔形图吗?还是有其他方法可以实现这样的目标?

However, I need to print these charts in black and white. It would be much more useful to be able to put textures on the bars and wedges, similar to the Line2D marker property which is available for drawing lines. Can I maybe fill the bars and wedges with these markers in a consistent way? Or is there any other way to achieve something like that?

推荐答案

使用bar(),您可以直接使用阴影线(带有某些后端):

With bar(), you can directly use hatches (with some backends): http://matplotlib.org/examples/pylab_examples/hatch_demo.html:

通过将hatch参数添加到对bar()的调用中,可以正常工作.

It works by adding the hatch argument to your call to bar().

对于pie(),它没有hatch关键字.相反,您可以获取各个饼图补丁并向其添加阴影:您可以通过以下方式获得补丁:

As for pie(), it does not have a hatch keyword. You can instead get the individual pie chart patches and add hatches to them: you get the patches with:

patches = pie(…)[0]  # The first element of the returned tuple are the pie slices

然后将阴影线应用于每个切片(补丁):

then you apply the hatches to each slice (patch):

patches[0].set_hatch('/')  # Pie slice #0 hatched.

(舱口列表位于 https://matplotlib.org/api/_as_gen/matplotlib.patches.Patch.html#matplotlib.patches.Patch.set_hatch ).

然后您将更改应用到:

pyplot.draw()

这篇关于如何为条形和楔形添加纹理?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆