如何使用 pandas 绘制阴影线? [英] How do I plot hatched bars using pandas?

查看:533
本文介绍了如何使用 pandas 绘制阴影线?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过填充图案而不是(只是)颜色来实现差异化.如何使用熊猫呢?

I am trying to achieve differentiation by hatch pattern instead of by (just) colour. How do I do it using pandas?

在matplotlib中可以通过传递hatch可选参数来实现,如

It's possible in matplotlib, by passing the hatch optional argument as discussed here. I know I can also pass that option to a pandas plot, but I don't know how to tell it to use a different hatch pattern for each DataFrame column.

df = pd.DataFrame(rand(10, 4), columns=['a', 'b', 'c', 'd'])
df.plot(kind='bar', hatch='/');

对于颜色,此处.孵化有类似的东西吗?还是可以通过修改plot返回的Axes对象来手动设置它?

For colours, there is the colormap option described here. Is there something similar for hatching? Or can I maybe set it manually by modifying the Axes object returned by plot?

推荐答案

这有点hacky,但是有效:

This is kind of hacky but it works:

df = pd.DataFrame(np.random.rand(10, 4), columns=['a', 'b', 'c', 'd'])
ax = plt.figure(figsize=(10, 6)).add_subplot(111)
df.plot(ax=ax, kind='bar', legend=False)

bars = ax.patches
hatches = ''.join(h*len(df) for h in 'x/O.')

for bar, hatch in zip(bars, hatches):
    bar.set_hatch(hatch)

ax.legend(loc='center right', bbox_to_anchor=(1, 1), ncol=4)

这篇关于如何使用 pandas 绘制阴影线?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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