【问题标题】:How to set same colors for same indexes in different charts in matplotlib and seaborn如何在 matplotlib 和 seaborn 的不同图表中为相同的索引设置相同的颜色
【发布时间】:2019-06-12 12:59:35
【问题描述】:

我正在尝试在两个图上绘制熊猫数据框。一个带有 matplotlib pyplot 饼图,另一个带有 seaborn barchart。在每个图表上,我对数据框进行了排序,但基于不同的列。此外,每个图表都代表数据帧排序所依据的相应值。因此,两个图表中的行顺序是不同的。这样,数据框中的相同索引(或类别)在图表上以不同的颜色出现,令人困惑。如何解决此问题,以便在不同图表上使用相同颜色的索引?

我的代码:

df = pd.DataFrame({"Total":totals,"Infected": infected}, 
                   index=category).sort_values("Total", ascending=False)
fig, ax = plt.subplots(ncols=2, nrows=1,figsize=(20,8))

#creating a pie chart with conditional explode option
threshold = new_train.shape[0]*threshold

if explode==-1:
    ax[0].pie(df[df["Total"]>threshold]["Total"], 
              labels=df[df["Total"]>threshold].index.values, 
              autopct='%1.1f%%',shadow=False, startangle=rotation,  
              textprops={'fontsize': 15})
else:
    ax[0].pie(df[df["Total"]>threshold]["Total"], 
              labels=df[df["Total"]>threshold].index.values, 
              autopct='%1.1f%%',shadow=False, startangle=rotation, 
              textprops={'fontsize': 15}, explode=explode)
ax[0].axis('equal')
ax[0].set_title(col_name)

#created a sorted bar chart
newdf = df[df["Total"]>threshold]
newdf.sort_values("Infected", ascending=False, inplace=True)
ax[1].set_xticklabels(category,rotation=45, horizontalalignment='right')
ax[1].set_title('Infected fractions')
ax[1] = sns.barplot(x=newdf.index, y="Infected",data=newdf, 
order=newdf.index)#, orient='h')
plt.show()

例如,1.1.15200.1 在饼图中为蓝色,但在条形图中为橙色。

【问题讨论】:

    标签: python matplotlib bar-chart seaborn pie-chart


    【解决方案1】:

    您必须制作一个字典,为您正在使用的标签的值查找适当的颜色。下面是一个简化版本,希望涵盖您对数据所做的所有事情:

    # a dataframe from a list of lists 
    el = [['RED', 50, 1], 
          ['GREEN', 30, 2], 
          ['BLUE', 7, 3], 
          ['YELLOW', 3, 4], 
          ['ORANGE', 9, 5], 
          ['BLACK', 1, 6]]
    df = pd.DataFrame(el)
    
    # since you are not using the entire dataframe
    df_thres = df[df[1] > 1]
    
    # make a fixed length color map manually
    c = ['red', 'green', 'blue', 'yellow', 'orange']
    clist1 = {i:j for i, j in zip(df_thres[0].values, c)}
    
    # make an arbitrary-length colormap
    cm = plt.get_cmap('rainbow')
    c = [cm(1.0 * i/len(df_thres)) for i in range(len(df_thres))]
    clist2 = {i:j for i, j in zip(df_thres[0].values, c)}
    
    
    fig, ax = plt.subplots(nrows=2, ncols=2, figsize=(11, 8))
    
    # manual colors
    
    # PIE PLOT: sort on the third column
    newdf = df_thres.sort_values(2, ascending=False)
    ax[0, 0].pie(newdf[2], labels=newdf[0], colors=[clist1[i] for i in newdf[0].values])
    ax[0, 0].axis('square') # matplotlib 2.x only
    
    # BAR PLOT: sort on the second column    
    newdf = df_thres.sort_values(1, ascending=False)
    ax[0, 1].bar(newdf[0], newdf[1], color=[clist1[i] for i in newdf[0].values])
    
    #----------------
    
    # arbitrary-length generated colorlist
    newdf = df_thres.sort_values(2, ascending=False)
    ax[1, 0].pie(newdf[1], labels=newdf[0], colors=[clist2[i] for i in newdf[0].values])
    ax[1, 0].axis('square') # matplotlib 2.x only
    
    newdf = df_thres.sort_values(1, ascending=False)
    ax[1, 1].bar(newdf[0], newdf[1], color=[clist2[i] for i in newdf[0].values])
    

    这是我得到的输出。下排的颜色不是标记的颜色,但它们是一致的。 (另外,请注意,我在这里的两个图都使用 matplotlib)

    【讨论】:

    • 谢谢@krm。我得到了答案,但我有一个建议,以便您准确地回答我的问题。我在两个图表中显示了两个不同的独立值。但是,您的图表是一个值的代表,只是以不同的方式排序。因此,对于第一行,按降序对数据进行排序,并使用 newdf[2] 提供饼图。我将编辑我的帖子以使其更清晰。
    猜你喜欢
    • 2016-02-04
    • 1970-01-01
    • 1970-01-01
    • 2022-01-27
    • 2021-07-30
    • 2020-07-26
    • 2015-05-01
    • 1970-01-01
    • 2020-05-26
    相关资源
    最近更新 更多