如何使用熊猫绘制阴影条?

met*_*mit 16 python plot matplotlib pandas

我试图通过填充图案而不是(仅)颜色来实现差异化.我怎么用熊猫来做?

在matplotlib中,通过传递这里hatch讨论的可选参数是可能的.我知道我也可以将该选项传递给大熊猫,但我不知道如何告诉它为每列使用不同的阴影图案.plotDataFrame

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

在此输入图像描述

对于颜色,有这里colormap描述的选项.孵化有类似的东西吗?或者我可以通过修改返回的对象手动设置它吗?Axesplot

beh*_*uri 19

这有点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)

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)
Run Code Online (Sandbox Code Playgroud)

酒吧

  • 而不是过滤`ax.get_children()`,可能只需将条形码作为`ax.patches`来访问. (2认同)

Leo*_*rdo 6

这段代码让你在定义模式时有更多的自由,所以你可以有“//”等。

bars = ax.patches
patterns =('-', '+', 'x','/','//','O','o','\\','\\\\')
hatches = [p for p in patterns for i in range(len(df))]
for bar, hatch in zip(bars, hatches):
    bar.set_hatch(hatch)
Run Code Online (Sandbox Code Playgroud)