【问题标题】:Stacked bar chart , stacking by genre of games堆积条形图,按游戏类型堆积
【发布时间】:2021-01-05 18:35:15
【问题描述】:

我有一个视频游戏销售数据,我能够按年份绘制视频游戏销售图表,但是我想做一个堆叠图表,这意味着人们会知道在某一年有多少动作游戏销售射击游戏销售策略游戏销售等 非常感谢您的帮助

Sales by Year

样本数据:

,Year,Genre,Global_Sales
0,2006.0,Sports,82.74
1,1985.0,Platform,40.24
2,2008.0,Racing,35.82
3,2009.0,Sports,33.0
4,1996.0,Role-Playing,31.37
5,1989.0,Puzzle,30.26
6,2006.0,Platform,30.01
7,2006.0,Misc,29.02
8,2009.0,Platform,28.62
9,1984.0,Shooter,28.31
10,2005.0,Simulation,24.76
11,2005.0,Racing,23.42
12,1999.0,Role-Playing,23.1
13,2007.0,Sports,22.72
14,2009.0,Sports,22.0
15,2010.0,Misc,21.82
16,2013.0,Action,21.4
17,2004.0,Action,20.81
18,1990.0,Platform,20.61
19,2005.0,Misc,20.22


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

df = pd.read_csv('vgsales.csv')

df.dropna(subset = ['Year'], inplace=True)
df['Year']=df['Year'].astype(int)
df['Year']= df['Year'].astype(str)
df.set_index(['Year'],inplace=True)

df = df.groupby(['Year']).agg({'Global_Sales':np.sum}) #gives sales for each year

# df= df.groupby(['Year','Genre']).agg({'Global_Sales':np.sum}) # gives sales by each genre 
# gf = df.groupby('Genre')

# df.reset_index(inplace=True)

# df.sort_values(df.index, inplace=True)
# plt.bar(df.index.get_level_values(0), df['Global_Sales'])

lab = df.index.get_level_values(0)
y = df['Global_Sales']
x = np.arange(len(lab))
plt.figure(figsize=(10,10))

plt.bar(x,y)
plt.title('Video Game Sales in Past Years', fontsize=20)
plt.xlabel('Years')
plt.ylabel('Sales (Millions)')
plt.xticks(x, lab, rotation = 'vertical')
plt.show()

【问题讨论】:

    标签: python pandas matplotlib bar-chart


    【解决方案1】:

    使用来自Kaggle: Video Games Sales的数据

    试试:

    df = pd.read_csv('data/kaggle/video_game_sales/vgsales.csv')
    
    dfg = df.groupby(['Year','Genre']).agg({'Global_Sales':np.sum})
    
    ax = dfg['Global_Sales'].unstack().plot.bar(stacked=True, figsize=(10,10))
    plt.legend(title='Game Genre', bbox_to_anchor=(1.05, 1), loc='upper left')
    ax.set_title('Global Sales')
    ax.set_ylabel('Sales (Millions)')
    ax.set_xlabel('Years')
    

    【讨论】:

      猜你喜欢
      • 2017-02-26
      • 2014-02-09
      • 2020-09-14
      • 2019-01-14
      • 2011-09-18
      • 2011-08-14
      • 2022-01-22
      • 2018-01-17
      相关资源
      最近更新 更多