【问题标题】:Pandas bar plot show only non zero legend熊猫条形图仅显示非零图例
【发布时间】:2019-03-14 23:37:29
【问题描述】:

我有一个 pandas 数据框 df1,如下所示:

Sample_names   esv0   esv1   esv2  esv3   ...    esv918  esv919  esv920  esv921

pr1gluc8NH1     635      0   6222     0   ...         0       0       0       0
pr1gluc8NH2    3189     75   9045     0   ...         0       0       0       0
pr1gluc8NHCR1     0   2152  12217     0   ...         0       0       0       0
pr1gluc8NHCR2     0  17411   1315     0   ...         0       1       0       0
pr1sdm8NH1      365      7   4117    32   ...         0       0       0       0
pr1sdm8NH2     4657     18  13520     0   ...         0       0       0       0
pr1sdm8NHCR1      0    139   3451     0   ...         0       0       0       0
pr1sdm8NHCR2   1130   1439   4163     0   ...         0       0       0       0

如您所见,有许多零值。我想绘制一个堆积条形图:

df1.plot(kind='bar',stacked=True)

这很好用,并给出了正确的条形图。但是这个图例很大,因为它为所有 922 值创建了一个图例。每个 Sample_names 只有大约 40-50 个非零值;所以原则上图例可以更小。有没有办法让它只打印非零值的图例? 我将不胜感激任何帮助。

注意:如果有帮助,我创建了一个字典,其中每个元素都是一个 sample_names 及其非零列的数据框。例如,我的字典 v 有 8 个元素,每个元素都是一个数据框。 v[0] 看起来像

Index       pr1gluc8NH1
esv2          6222
esv9          4879
esv27         2050

依此类推(它有 43 个非零行)。

v[1] 是相同的方式,但用于下一个样本。如果可能的话,我也可以使用这本字典来制作情节。

【问题讨论】:

  • 谢谢。 label='_nolegend_' 不起作用。 (我只是像这样包含它:df1.plot(kind='bar',stacked=True, label='_nolegend_'))我试图找出另一个答案,for 循环可以用于我的数据框。

标签: python pandas dataframe bar-chart


【解决方案1】:

我在创建堆积条形图时遇到了类似的问题,并且只标记了一些数据以避免出现巨大的图例。我使用子图解决了这个问题,并根据条件在 for 循环中手动设置标签,灵感来自 herehere

我重新创建了一个较小版本的数据框:

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

data = [['pr1gluc8NH1',635,0,6222,0,23,3,543],['pr1gluc8NH2',3189,75,9045,0,55,5,66],['pr1gluc8NHCR1',0,2152,12217,0,43,67,43],['Bpr1gluc8NHCR2',0,17411,1315,0,889,56,0],['pr1sdm8NH1',365,7,4117,32,765,34,0]]
df = pd.DataFrame(data, columns = ['Sample_names','esv0','esv1','esv2','esv3','esv4','esv5','esv6'])
df = df.set_index('Sample_names')

print(df)
                esv0   esv1   esv2  esv3  esv4  esv5  esv6
Sample_names                                              
pr1gluc8NH1      635      0   6222     0    23     3   543
pr1gluc8NH2     3189     75   9045     0    55     5    66
pr1gluc8NHCR1      0   2152  12217     0    43    67    43
Bpr1gluc8NHCR2     0  17411   1315     0   889    56     0
pr1sdm8NH1       365      7   4117    32   765    34     0

然后我遍历数据框并将每列的值绘制为堆叠在先前条形顶部的条形图。只有没有零值的列才会在图例中添加标签。

mycolors = sns.color_palette(n_colors=10) #set color palette with seaborn colors
f, ax1 = plt.subplots()
bot_array = [] #need this for representing values already plotted
labels = df.columns.values.tolist() #get labels from column names

#Create base bar separately because it doesn't require 'bottom' value
col = df.iloc[:,0].to_list()
if float(0) in col:
  ax1.bar(range(len(col)), col, label="", color=mycolors[0])
  bot_array = np.array(col)
else:
  ax1.bar(range(len(col)), col, label=labels[0], color=mycolors[0])
  bot_array = np.array(col)

#Loop over dataframe and add each column as a new colored bar with label only if there are no zero values in that column
for i in range(1,len(df.columns)):
  cur_color = mycolors[i]
  col = df.iloc[:,i].to_list()
  if float(0) in col:
    ax1.bar(range(len(col)), col, bottom=bot_array, label="", color=mycolors[i])
    bot_array = np.array(col)+bot_array
  else:
    ax1.bar(range(len(col)), col, bottom=bot_array, label=labels[i], color=mycolors[i])
    bot_array = np.array(col)+bot_array
ax1.set_ylabel('Count')
plt.legend(loc='upper right')

Stacked bar plot with limited legend

【讨论】:

    猜你喜欢
    • 2023-03-16
    • 2020-10-22
    • 2017-12-30
    • 2020-02-05
    • 2018-04-07
    • 2013-10-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多