使用seaborn和matplotlib的堆积条形图 [英] stacked bar chart using seaborn and matplotlib

查看:329
本文介绍了使用seaborn和matplotlib的堆积条形图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是熊猫和matplotlib的新手,正在尝试实现以下目标.我有一个如下所示的数据框,它实际上是根据比赛日期列出球员表现的

I am new to pandas and matplotlib and trying to accomplish following. I have a data frame as shown below which is actually a listing the performance of players based on match date

name          runs  match_date  player_id
Dockrell, G H   0   2018-06-17  3752
Stirling, P R   81  2018-06-17  3586
O'Brien, K J    28  2018-06-17  3391
McCarthy, B J   0   2018-06-17  4563
Poynter, S W    0   2018-06-17  4326
Poynter, S W    2   2018-06-17  4326
McCarthy, B J   0   2018-06-17  4563
Shannon, J N K  5   2018-06-17  4219
Shannon, J N K  6   2018-06-17  4219
Stirling, P R   51  2018-06-17  3586

这是我根据以下代码创建的数据子集

This is a subset of data that I have created based on following code

match_performance = dataFrame[['name','runs','match_date','player_id']].sort_values('match_date',ascending=False).groupby('player_id').head(5)
sns.set_context({"figure.figsize": (10, 4)})
ax = sns.barplot(x="name", y="runs",  data=match_performance)
ax.set_xticklabels(ax.get_xticklabels(), rotation=90) 

我需要将其绘制为堆积条形图或分组条形图,以根据我在数据框中拥有的玩家ID来显示最近5场比赛中的玩家表现,但我不确定如何根据需要绘制此数据.

I need to plot this either as stacked bar or grouped bar to display performance of players in there last 5 matches based on player id which I have in the dataframe but I am not sure how to go about plotting this data as required.

推荐答案

虽然这是一个较旧的问题,但我在寻找解决方案时发现了它,所以希望对您有所帮助.对于seaborn来说,实现堆叠的条形有点棘手,但这应该可以解决问题

While this is an older question I found it while looking for a solution, so I hope this may help someone. Achieving stacked bars are a bit tricky with seaborn, but this should do the trick

import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
import matplotlib.patches as mpatches

def stacked_chart_sns(df, x, y, group_by, palette):
    array_of_dfs = []
    w_0 = None
    for u in df[group_by].unique():
        w = df[df[group_by] == u].copy()
        if w_0 is not None:
            w = w.merge(w_0, how='outer').fillna(0)
            w[group_by] = u
            w[y] = w.apply(lambda x: x[y] + x['y_prev'], axis=1)
            w = w.drop(columns=['y_prev'])
        array_of_dfs += [w]
        w_0 = w.drop(columns=[group_by]).rename(columns={y:'y_prev'}).copy()

    patches = []
    for i, d in enumerate(array_of_dfs[::-1]):
        sns.barplot(x=x, y=y, data=d, color=palette[i])
        patches += [mpatches.Patch(label=list(df[group_by].unique())[::-1][i], color=palette[i])]

    plt.legend(handles=patches, loc = 'upper left', ncol=1, labelspacing=1)
    plt.show()

### use it with - for the example data in the question:
stacked_chart_sns(match_performance, 'match_date', 'runs', 'player_id', sns.color_palette("Spectral"))

这篇关于使用seaborn和matplotlib的堆积条形图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆