带有 matplotlib 的重叠/隐藏条上的彩色阴影条图

Colored hatched bar plot on overlapping / hidden bars with matplotlib

目前,我像这样创建条形图来比较两个结果

plt.figure(figsize=(12,8))
result_df.sym.plot(kind='barh', edgecolor='none')
result_df.non_sym.plot(kind='barh', edgecolor='none',color='orange')

这导致了这个

我想证明蓝色结果通常优于黄色结果。但对于某些类别,它们不存在,因此它们根本不可见。 我现在想要的是这样的:

我用 powerpoint 快速添加了这个,但当然我想直接用 matplotlib/seaborn/pandas 生成它。但我不知道如何。 matplotlib里面有hatched这个参数但是没有达到我想要的效果

sebko_iic。在黄色下面画蓝色,或者在蓝色下面画黄色怎么样? 只需在此行中设置正确的偏移量: 细节: https://scipy-cookbook.readthedocs.io/items/Matplotlib_MultilinePlots.html

使用 matplotlib,您可以通过仅使用影线绘制带有第一个数据集的第三个条来获得此效果:

df = pd.DataFrame({'A': [1, 2, 3, 4],
                   'B': [4, 3, 2, 1]
                  })

import matplotlib as mpl
mpl.rcParams['hatch.linewidth'] = 10
mpl.rcParams['hatch.color'] = '#1F77B4'
df['A'].plot(kind='barh', edgecolor='none', color='#1F77B4')
df['B'].plot(kind='barh', edgecolor='none',color='orange')
df['A'].plot(kind='barh', color='none', hatch='/')

输出:

df['A'].plot(kind='barh', edgecolor='none', color='#1F77B4')
df['B'].plot(kind='barh', edgecolor='none',color='orange')
df['A'].mask(df['B'].lt(df['A'])).plot(kind='barh', color='none', hatch='/')

输出:

对于较薄的舱口:

import matplotlib as mpl
mpl.rcParams['hatch.linewidth'] = 3
mpl.rcParams['hatch.color'] = '#1F77B4'
df['A'].plot(kind='barh', edgecolor='none', color='#1F77B4')
df['B'].plot(kind='barh', edgecolor='none',color='orange')
df['A'].mask(df['B'].lt(df['A'])).plot(kind='barh', color='none', hatch='//')

输出: