【问题标题】:Color based on categorical variable in Python SNS Barplot基于 Python SNS Barplot 中分类变量的颜色
【发布时间】:2019-01-30 20:13:43
【问题描述】:

我有一个如下的数据框(经过大量预处理后获得)

请找代码

{'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}}

我正在使用下面的代码来创建我的 SNS 条形图,但它的抛出错误,在图表之后我想显示标准偏差

df1 = df1.groupby(['token','variable']).agg({'value': 'mean'})
df1.reset_index(inplace=True)
g=sns.barplot(x='token',y='value',data=df1, color='variable')
plt.show()

我想要的输出如下

数据框

Dataframe Expected Output

但是,我收到了一个错误代码

ValueError: Invalid RGBA argument: 'variable'

【问题讨论】:

    标签: python pandas bar-chart seaborn


    【解决方案1】:

    首先错误在于color 参数。 hue 参数可以满足您的需求。检查下面的代码:

    #For sorting the values in descending order
    df.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.barplot(x='token',y='value',data=df, hue='variable',ax=ax)
    
    #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')
    
    #To save the plot as 'SO.png'    
    plt.savefig('SO.png',dpi=100,bbox_inches='tight')
    plt.show()
    

    图例代码来自link

    剧情如下:

    【讨论】:

    • 感谢@Sandeep,我还使用下面的图表在图表顶部添加了值,但是我将如何按预期输出从多到少对其进行排序-------------- -------------------------- 对于 ax.patches 中的 p: ax.annotate("%.2f" % p.get_height(), ( p.get_x() + p.get_width() / 2., p.get_height()), ha='center', va='center', fontsize=11, color='gray', xytext=(0, 20 ), textcoords='偏移点')
    • 非常感谢 Sandeep,我无法弄清楚的最后一件事是如何在预期输出中显示标准偏差胡须。尝试到处搜索相同的
    • @Artika 如果对你有帮助的话please upvote and also accept the solution。很高兴能提供帮助。
    • Sandeep 是这样做的吗,作为导师,您对我非常有帮助。如果我要使用回归线制作散点图,那么我将在现有图表中进行哪些更改
    • @Artika For scatterplot with regression line。但是上面的一些功能将不起作用。
    猜你喜欢
    • 1970-01-01
    • 2020-10-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-18
    • 1970-01-01
    • 2020-12-23
    • 1970-01-01
    相关资源
    最近更新 更多