【问题标题】:How to plot certain rows of a pandas dataframe如何绘制熊猫数据框的某些行
【发布时间】:2016-02-17 04:32:22
【问题描述】:

我有这个示例数据框:

      animal gender     name  first  second  third
0     dog      m      Ben      5       6      3
1     dog      f    Lilly      2       3      5
2     dog      m      Bob      3       2      1
3     cat      f     Puss      1       4      4
4     cat      m  Inboots      3       6      5
5    wolf      f     Lady    NaN       0      3
6    wolf      m   Summer      2       2      1
7    wolf      m     Grey      4       2      3
8    wolf      m     Wind      2       3      5
9    lion      f     Elsa      5       1      4
10   lion      m    Simba      3       3      3
11   lion      f     Nala      4       4      2

现在,我怀疑我可能需要一些分层索引,但我在 Pandas 中还没有那么远。但是,我真的需要用它做一些(显然太高级)的事情,并且还没有弄清楚如何去做。 基本上,在这种情况下,我最终想要的是一个图(可能是一个散点图,尽管现在一条线也可以使用)。

1) 我想要一个包含 4 个子图的图形 - 每只动物一个子图。每个子图的标题应该是动物。

2)在每个子图中,我想绘制数字(例如每年出生的幼崽数量),即给定行的“第一”、“第二”和“第三”的值并给出它是一个标签,将在图例中显示“名称”。对于每个子图(每只动物),我想分别绘制雄性和雌性(例如蓝色的雄性和红色的雌性),此外,还要绘制动物的平均值(即每列中的平均值)给定的动物)黑色。

3) 注释:例如针对 1,2,3 绘制它 - 指的是列号, 因此,例如,对于标题为“狗”的第一个子图,我想绘制类似 plt.plot(np.array([1,2,3]),x,'b', np.array([1,2,3]),y,'r', np.array([1,2,3]), np.mean(x,y,axis=1),'k') 的东西,其中 x (在第一种情况下)为 5、6、3,而这个蓝色图的图例将显示“本” ', y 将是 2,3,5,红色图的图例将显示 'Lilly',黑色图将是 3.5、4.5、4,在图例中我将定义它是“平均值”(对于每个子图)。

我希望我说得够清楚。我知道如果没有看到最终的数字,可能很难想象它,但是......好吧,如果我知道如何制作它,我不会问......

因此,总而言之,我想遍历不同级别的数据框,将动物放在不同的子图上,比较雄性和雌性以及每个子图中它们之间的平均值。

我的实际数据框要大得多,所以在理想情况下,我想要一个健壮但易于理解的解决方案(对于编程初学者)。

要了解子图应该是什么样子,这是 excel 中的产品:

【问题讨论】:

  • 使用for i, group in df.groupby('animal'): 并循环绘制。没有答案,因为我有点赶时间。
  • 我想我的问题在这里得到了部分回答:stackoverflow.com/questions/14300137/… 但是,我对索引和循环这些多维数据并不完全有信心,尤其是绘制行,而不是列......跨度>
  • 感谢钦梅!有时间的话,能多解释一下吗? (例如,我如何处理 groupby 的两个参数 - “i”是什么?以及如何处理分组对象的行。)
  • 刚刚在数据框中添加了一些行,使其更加复杂以确保其正常工作。
  • 我打算链接到文档,但它们的描述性不是很好。 groupby 中的 i 是组的“名称”,在本例中为 wolflion 等。

标签: python pandas matplotlib seaborn


【解决方案1】:

我不确定我是否理解你的意思。 但我认为您需要将您的数据框转换为长格式或tidy format,因为使用该格式您将对其进行的许多操作会更容易,首先是根据分类变量制作图。

df 是您的数据框,要将其转换为整洁的格式,只需使用:

df2 = pd.melt(df, id_vars=["animal","gender","name"])
df2
  animal gender     name variable  value
0    dog      m      Ben    first    5.0
1    dog      f    Lilly    first    2.0
2    dog      m      Bob    first    3.0
3    cat      f     Puss    first    1.0
4    cat      m  Inboots    first    3.0
...
31   wolf     m     Grey    third    3.0
32   wolf     m     Wind    third    5.0
33   lion     f     Elsa    third    4.0
34   lion     m    Simba    third    3.0
35   lion     f     Nala    third    2.0

然后(几乎)一切都变得简单,只需使用 seaborn 如下:

g = sns.factorplot(data=df2, # from your Dataframe
                   col="animal", # Make a subplot in columns for each variable in "animal"
                   col_wrap=2, # Maximum number of columns per row 
                   x="variable", # on x-axis make category on the variable "variable" (created by the melt operation)
                   y="value", # The corresponding y values
                   hue="gender", # color according to the column gender
                   kind="strip", # the kind of plot, the closest to what you want is a stripplot, 
                   legend_out=False, # let the legend inside the first subplot.
                   )

那么就可以提升整体美感了:

g.set_xlabels("year")
g.set_titles(template="{col_name}") # otherwise it's "animal = dog", now it's just "dog"
sns.despine(trim=True) # trim the axis.

要添加平均值,恐怕您必须手动完成,但是,如果您有更多数据,您也可以考虑使用箱线图或小提琴图,您可以在条形图的顶部使用它们,顺便说一句。

我邀请您查看Seaborn's documentation 以进一步改进您的情节。

HTH

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-02-10
    • 1970-01-01
    • 1970-01-01
    • 2019-07-21
    • 1970-01-01
    • 2018-09-19
    • 2016-09-16
    • 2015-03-29
    相关资源
    最近更新 更多