我有一个 pandas 数据框,其中包含如下所示的分类数据:
cat DM3_r DM3_r_err
133 stuff 9.908949 0.442363
1347 foo 1.156828 0.130174
132 bar 0.818709 0.593341
1350 stack 0.798348 0.089866
977 over 0.724274 0.462868
1054 flow 0.546665 0.538208
1228 run 0.425070 0.571659
1009 run 0.316554 0.259385
1109 yadG 0.304657 0.401482
我正在使用 seaborn 的 stripplot 来可视化数据:
seaborn.stripplot(df.DM3_r, df.cat, size=7, orient="h",
palette="Reds_r", edgecolor="gray", ax=ax.flat[i], linewidth=.5)
在这种情况下,对于不同的 “cat” 组。这很好用。但是,我想添加错误 "DM3_r_err"。
我已经尝试使用 matplotlib 中的 errorbar 在顶部添加一个 errorbar 图,但我无法提取 seaborn 图表中点的位置。这可能是因为我使用的是子图。
有直接使用seaborn的方法吗?
请您参考如下方法:
可能有更智能的方法从 Pandas.DataFrame 中选择数据,但像这样的方法可行:
import pandas as pd
import matplotlib.pylab as pl
import seaborn
import numpy as np
pl.close('all')
data = [['hsdM', 3.908949, 1.442363],
['lolD', 1.156828, 0.456434],
['lolD', 3.156828, 0.230174],
['acrB', 0.546665, 0.538208],
['msbA', 0.425070, 0.571659],
['msbA', 2.425070, 1.571659],
['emrA', 0.316554, 1.259385],
['yadG', 0.304657, 0.401482]]
df = pd.DataFrame(data, columns=['gene','DM3_r','DM3_r_err'])
pl.figure()
ax = pl.subplot(111)
# Everywhere below you would have to replace `ax` with your `ax.flat[i]`
sp = seaborn.stripplot(df.DM3_r, df.gene, size=7, orient="h",
palette="Reds_r", edgecolor="gray", ax=ax, linewidth=.5)
for y,ylabel in zip(ax.get_yticks(), ax.get_yticklabels()):
f = df['gene'] == ylabel.get_text()
ax.errorbar(df.DM3_r[f].values, np.ones_like(df.DM3_r[f].values)*y, xerr=df.DM3_r_err[f].values, ls='none')
