【问题标题】:How to set a color to different sub groups in barplot seaborn如何在 barplot seaborn 中为不同的子组设置颜色
【发布时间】:2021-07-06 14:09:16
【问题描述】:

我有一个条形图,其中 x 轴的值范围从 1 到 100。 我想用特定颜色标记每组 50(1-50,51-100 等)。以下是我目前标记它们的方法,但我不知道如何对它们进行分组-

ylabels = new_merged['time_A'].values.tolist()
xlabels = new_merged['Index'].to_list()
plt.figure(figsize=(100,50))
sns.barplot(xlabels, ylabels, alpha=0.8)
plt.title('Grader_A(Non-Medical)',fontsize=56)
plt.ylabel('Time taken per Image', fontsize=54)
plt.xlabel('Images', fontsize=54)
plt.savefig('Marco.png')
plt.show()

【问题讨论】:

    标签: python matplotlib seaborn visualization


    【解决方案1】:

    您可以为每个条形的颜色创建一个列表['red', 'red', .... 'blue', 'blue'...],其中'red' 重复50 次,'blue' 重复50 次等等...,然后从这个列表中创建一个seaborn color palette并将其传递给sns.barplot 方法。

    import numpy as np
    import pandas as pd
    
    import matplotlib.pyplot as plt
    import seaborn as sns
    
    ## recreate some data
    np.random.seed(42)
    df_merged = pd.DataFrame({'time_A':np.random.normal(5.0, 1.0, 200)})
    
    ## this produces a palette that labels each group of 50 bars
    palette = [color for color in ['red', 'blue', 'green', 'purple'] for _ in range(50)] 
    
    plt.figure(figsize=(100,50))
    sns.barplot(x=df_merged.time_A, y=df_merged.index, palette=sns.color_palette(palette))
    plt.savefig('Marco.png')
    

    【讨论】:

      猜你喜欢
      • 2015-09-13
      • 1970-01-01
      • 2020-07-26
      • 1970-01-01
      • 1970-01-01
      • 2021-03-04
      • 2021-06-16
      • 2019-06-12
      • 2020-02-04
      相关资源
      最近更新 更多