Seaborn barplot 中的误差条问题 - Python

瓦巴夫·辛格

(已经看过类似的问题,但他们没有回答这个问题)

我有一个结构如下的数据框 df1

{'token': {0: '180816_031', 1: '180816_031', 2: '180816_031', 3: '180816_031', 4: '180816_031', 5: '180816_031', 6: '180816_031', 7: '180816_031', 8: '180816_031', 9: '180816_031'}, 'variable': {0: 'Unnamed: 0', 1: 'adj_active_polymerase', 2: 'adj_functional_sequencing_pores', 3: 'adj_high_quality_reads', 4: 'adj_single_pores', 5: 'cell_mask_bilayers_sum', 6: 'num_align_high_quality_reads', 7: 'num_total_cells', 8: 'potential_pore', 9: 'short_pass'}, 'value': {0: 21.0, 1: 615850.51515151514, 2: 615850.51515151514, 3: 486008.39393939392, 4: 803784.06060606055, 5: 1665347.5757575757, 6: 468638.03030303027, 7: 2097152.0, 8: 1158527.0, 9: 2067189.2424242424}}

我使用下面的代码重新创建我的数据,然后显示条形图

    df1 = df.groupby(['token','variable']).agg({'value':['mean','std']})
    df1.reset_index(inplace=True)
    df1.sort_values('value',inplace=True,ascending=False)
    
    fig,ax = plt.subplots()
    fig.set_size_inches(16,8)
    #to get different colors for each of the variable assign the variable to hue
    g = sns.factorplot(x='token',y='value',data=df1, hue='variable',ax=ax,  yerr='std')
   #g.map_dataframe(plt.errorbar, x="followup (months)", y="probability", yerr='sd')
    #Code for to put legend outside the plot
    box = ax.get_position()
    ax.set_position([box.x0, box.y0, box.width * 0.8, box.height])
    # Put a legend to the right of the current axis
    ax.legend(loc='center left', bbox_to_anchor=(1, 0.5))
    # Adding respective values to the top of each bar
    for p in ax.patches: 
        ax.annotate("%d" % p.get_height(), (p.get_x() + p.get_width() / 2, p.get_height()),
                    ha='center', va='center', fontsize=11, color='black', xytext=(0, 10), 
                    textcoords='offset points',fontweight='bold')
    plt.show()

但我无法在输出中获得所需的误差条 预期产出

我在我的数据框中只使用 mean 之前生成了一个输出,但是我也需要错误栏,所以我想添加 std & 在 yerr 中使用它(在阅读了这么多之后)

请帮忙。

尼哈尔
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns


d = {'token': {361: '180816_031', 119: '180816_031', 101: '180816_031', 135: '180816_031', 292: '180816_031',
               133: '180816_031', 99: '180816_031', 270: '180816_031', 19: '180816_031', 382: '180816_031',
               414: '180816_031', 267: '180816_031', 218: '180816_031', 398: '180816_031', 287: '180816_031',
               155: '180816_031', 392: '180816_031', 265: '180816_031', 239: '180816_031', 237: '180816_031'},
     'station': {361: 'deneb', 119: 'callisto', 101: 'callisto', 135: 'callisto', 292: 'callisto', 133: 'deneb',
                 99: 'callisto', 270: 'callisto', 19: 'deneb', 382: 'callisto', 414: 'deneb', 267: 'callisto',
                 218: 'deneb', 398: 'callisto', 287: 'deneb', 155: 'deneb', 392: 'deneb', 265: 'callisto',
                 239: 'callisto', 237: 'callisto'},
     'cycle_number': {361: 'cycle09', 119: 'cycle06', 101: 'cycle04', 135: 'cycle01', 292: 'cycle04', 133: 'cycle05',
                      99: 'cycle06', 270: 'cycle07', 19: 'cycle04', 382: 'cycle08', 414: 'cycle04', 267: 'cycle10',
                      218: 'cycle07', 398: 'cycle08', 287: 'cycle09', 155: 'cycle08', 392: 'cycle06', 265: 'cycle02',
                      239: 'cycle09', 237: 'cycle07'},
     'variable': {361: 'adj_high_quality_reads', 119: 'short_pass', 101: 'short_pass', 135: 'cell_mask_bilayers_sum',
                  292: 'adj_active_polymerase', 133: 'cell_mask_bilayers_sum', 99: 'short_pass',
                  270: 'adj_active_polymerase', 19: 'Unnamed: 0', 382: 'adj_high_quality_reads',
                  414: 'num_align_high_quality_reads', 267: 'adj_active_polymerase', 218: 'adj_single_pores',
                  398: 'num_align_high_quality_reads', 287: 'adj_active_polymerase', 155: 'cell_mask_bilayers_sum',
                  392: 'num_align_high_quality_reads', 265: 'adj_active_polymerase', 239: 'adj_single_pores',
                  237: 'adj_single_pores'},
     'value': {361: 99704.0, 119: 2072785.0, 101: 2061059.0, 135: 1682208.0, 292: 675306.0, 133: 1714292.0,
               99: 2072785.0, 270: 687988.0, 19: 19.0, 382: np.nan, 414: 285176.0, 267: 86914.0, 218: 948971.0,
               398: 405196.0, 287: 137926.0, 155: 1830032.0, 392: 480081.0, 265: 951689.0, 239: 681452.0,
               237: 882671.0}}
df = pd.DataFrame(d)


g = sns.barplot('token', 'value', data=df, hue='variable', capsize=0.1)

df5 = pd.DataFrame(df.groupby(['variable'])['value'].mean().reset_index())
i = 0
for p in g.patches:
    height = p.get_height()

    g.text(p.get_x() + p.get_width() / 2.,
           height + 3,
           "%.3f" % df5.at[i, 'value'],
           ha="center")
    i += 1
plt.show()

在此处输入图片说明

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Python Seaborn Barplot栏不居中吗?

来自分类Dev

如何在Seaborn barplot Python中根据名称为单个栏着色

来自分类Dev

Seaborn Barplot:异类条形

来自分类Dev

如何在Seaborn barplot Python中根据名称将数据标签注释添加到单个栏

来自分类Dev

如何在seaborn barplot中添加更多网格?

来自分类Dev

如何在seaborn barplot中包括多个数据列?

来自分类Dev

barplot python中的双标签

来自分类Dev

将预先计算的误差线与Seaborn和Barplot一起使用

来自分类Dev

是否可以在seaborn.barplot中的每个单独的栏上添加阴影?

来自分类Dev

无法使用Seaborn Barplot绘制数据框

来自分类Dev

Seaborn的barplot bar和error bar的美学

来自分类Dev

seaborn barplot添加xticks作为色相

来自分类Dev

Seaborn barplot在绘图时混合了列值

来自分类Dev

Seaborn:如何将图例添加到seaborn barplot

来自分类Dev

如何在Seaborn barplot中添加一个分类列的色相(每个列的均值和std)?

来自分类Dev

python中的seaborn dataframe问题进行分组和计数

来自分类Dev

重新排序R的barplot()中的条

来自分类Dev

重新排序R的barplot()中的条

来自分类Dev

Matplotlib / Seaborn barplot-x轴上的字符串

来自分类Dev

使用seaborn barplot绘制宽格式的数据框

来自分类Dev

如何旋转Seaborn Barplot X轴刻度标签?

来自分类Dev

将子图与pyplot barplot和seaborn热图对齐

来自分类Dev

使用seaborn barplot绘制宽格式的数据框

来自分类Dev

如何在python中旋转Seaborn heatmap?

来自分类Dev

基于 Python 的 seaborn 中的比率的热图

来自分类Dev

barplot()中的订购栏

来自分类Dev

barplot()中的订购栏

来自分类Dev

R 3.1.1中的barplot

来自分类Dev

如何使用带有 Pandas 数据集的 Seaborn 修复 Barplot 错误(它不会让我对我的变量进行 barplot)

Related 相关文章

  1. 1

    Python Seaborn Barplot栏不居中吗?

  2. 2

    如何在Seaborn barplot Python中根据名称为单个栏着色

  3. 3

    Seaborn Barplot:异类条形

  4. 4

    如何在Seaborn barplot Python中根据名称将数据标签注释添加到单个栏

  5. 5

    如何在seaborn barplot中添加更多网格?

  6. 6

    如何在seaborn barplot中包括多个数据列?

  7. 7

    barplot python中的双标签

  8. 8

    将预先计算的误差线与Seaborn和Barplot一起使用

  9. 9

    是否可以在seaborn.barplot中的每个单独的栏上添加阴影?

  10. 10

    无法使用Seaborn Barplot绘制数据框

  11. 11

    Seaborn的barplot bar和error bar的美学

  12. 12

    seaborn barplot添加xticks作为色相

  13. 13

    Seaborn barplot在绘图时混合了列值

  14. 14

    Seaborn:如何将图例添加到seaborn barplot

  15. 15

    如何在Seaborn barplot中添加一个分类列的色相(每个列的均值和std)?

  16. 16

    python中的seaborn dataframe问题进行分组和计数

  17. 17

    重新排序R的barplot()中的条

  18. 18

    重新排序R的barplot()中的条

  19. 19

    Matplotlib / Seaborn barplot-x轴上的字符串

  20. 20

    使用seaborn barplot绘制宽格式的数据框

  21. 21

    如何旋转Seaborn Barplot X轴刻度标签?

  22. 22

    将子图与pyplot barplot和seaborn热图对齐

  23. 23

    使用seaborn barplot绘制宽格式的数据框

  24. 24

    如何在python中旋转Seaborn heatmap?

  25. 25

    基于 Python 的 seaborn 中的比率的热图

  26. 26

    barplot()中的订购栏

  27. 27

    barplot()中的订购栏

  28. 28

    R 3.1.1中的barplot

  29. 29

    如何使用带有 Pandas 数据集的 Seaborn 修复 Barplot 错误(它不会让我对我的变量进行 barplot)

热门标签

归档