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

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

问题描述

我试图通过填充图案而不是(仅)颜色来实现差异化.我如何使用熊猫来做到这一点?

在 matplotlib 中,通过传递 hatch 可选参数是可能的,正如讨论的 .孵化有类似的东西吗?或者我可以通过修改 plot 返回的 Axes 对象来手动设置它?

解决方案

这有点 hacky 但它有效:

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)条 = ax.patches舱口 = ''.join(h*len(df) for h in 'x/O.')对于酒吧,拉链舱口(酒吧,舱口):bar.set_hatch(舱口)ax.legend(loc='center right', bbox_to_anchor=(1, 1), ncol=4)

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

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='/');

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?

解决方案

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天全站免登陆