如何绘制pandas数据帧的某些行?

时间:2015-11-16 05:51:23

标签: python pandas matplotlib seaborn

我有这个示例数据框:

      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进行绘图 - 参考列号, 因此,例如,对于标题为“dog”的第一个子图,我想绘制像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并且该蓝色图的图例将显示'Ben',y将是2,3,5,红色情节的传说将显示'Lilly',黑色情节将是3.5,4.5,4,在传说中我会定义它是“意味着”(对于每个子图。)

我希望自己足够清楚。我明白,如果没有看到结果,可能很难想象它,但是......好吧,如果我知道怎么做,我就不会问......

总而言之,我想循环不同层次的数据框架,让动物在不同的子图上进行比较,男性和女性的比较以及每个子图中它们之间的平均值。

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

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

briefly outlined plot

1 个答案:

答案 0 :(得分:1)

我不确定我是否理解你的意思。 但我认为您需要将数据帧转换为longform格式或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.

stripplot seaborn

要添加平均值,您必须手动执行我担心,但是,如果您有更多数据,您可以考虑一个盒子图或小提琴图,您可以在stripplot顶部使用,顺便说一句。

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

HTH