python - Seaborn 条形图,误差线仅在一个方向上
问题描述
我正在使用 seaborn barplot 函数来绘制来自不同组的数据。是否可以仅在一个方向(仅向上但不能向下)绘制误差线?
这就是我所拥有的:
np.random.seed(2)
df = pd.DataFrame({'value': np.random.randint(0,100,20), 'group1': ['A','B']*10, 'group2': ['Y','Z']*5 + ['Z','Y']*5 })
fig, ax = plt.subplots(1,1,figsize=[5,6])
sns.barplot('group1','value','group2',data=df, ax=ax,capsize=.15, lw=1, edgecolor=".2", ci=68) # SEM errorbars
plt.show()
这就是我想要的:
谢谢!
解决方案
我在 seaborn 中看不到默认选项,但我们可以用zorder
fig, ax = plt.subplots(1,1,figsize=[5,6])
# plot the error bars
# notice the zorder
sns.barplot('group1','value','group2',
data=df,
ax=ax,capsize=.15, zorder=5,
ci=68) # SEM errorbars
# plot the mean bars
# notice the zorder
sns.barplot('group1','value','group2',
data=df,
ax=ax, lw=1, edgecolor=".2",
zorder=10,
ci=None) # no CI errorbars
# handle the duplicate legend
handles, labels = ax.get_legend_handles_labels()
ax.legend(handles[-2:], labels[-2:], title='group2')
plt.show()
输出:
推荐阅读
- assembly - 引导加载程序上的幻数 0x1BADB002 和 0xAA55 有什么区别
- jquery - JQuery - 高效代码编写问题
- qt - 更改 Qt 维护工具窗口的宽度?
- wordpress - WordPress 在自定义帖子类型的所有分类和术语中搜索
- ios - 如何在 Xcode 中编辑位于不同场景中的标签
- matlab - 如何将向量(沿第四维)分配给预先指定的下标
- c# - 以下返回值的有效 C# 数据类型是什么,或者如何访问内部属性?
- ruby-on-rails - Rails - 应用一系列范围
- c# - 当 CheckBox 未选中时,使其无法在特定时间 C#
- linked-list - 链表的第一个元素(例如list[0])是引用头还是第一个节点?