【发布时间】:2015-08-03 20:14:40
【问题描述】:
我想在 seaborn 中绘制因子图,但手动提供误差线,而不是让 seaborn 计算它们。
我有一个大致如下所示的 pandas 数据框:
model output feature mean std
0 first two a 9.00 2.00
1 first one b 0.00 0.00
2 first one c 0.00 0.00
3 first two d 0.60 0.05
...
77 third four a 0.30 0.02
78 third four b 0.30 0.02
79 third four c 0.10 0.01
我正在输出一个大致如下所示的图:
我正在使用这个 seaborn 命令来生成情节:
g = sns.factorplot(data=pltdf, x='feature', y='mean', kind='bar',
col='output', col_wrap=2, sharey=False, hue='model')
g.set_xticklabels(rotation=90)
但是,我不知道如何让 seaborn 使用“std”列作为误差线。不幸的是,重新计算相关数据帧的输出会非常耗时。
这有点类似于这个q: Plotting errors bars from dataframe using Seaborn FacetGrid
除了我不知道如何让它与 matplotlib.pyplot.bar 函数一起工作。
有没有办法将 seaborn factorplot 或 FacetGrid 与 matplotlib 结合使用?
谢谢!
【问题讨论】:
-
我认为链接问题将是最好的方法。
plt.bar有一个yerr参数应该会有所帮助。 -
谢谢@mwaskom,关于如何让它运行的任何提示?目前以下代码阻塞:
g = sns.FacetGrid(data=pltdf, col='output', col_wrap=6, sharey=False, hue='model') g.map(plt.bar, 'feature', 'mean', yerr='std') -
为混乱的代码道歉,似乎无法在评论部分很好地格式化。
-
我认为问题在于 bar 中的 yerr 参数不是位置参数,而是 kwarg。
-
请参阅rules 了解可映射的功能。您需要在
plt.bar周围编写一个瘦包装器,它接受yerr作为位置参数。
标签: python pandas matplotlib plot seaborn