当前位置:   article > 正文

Python数据分析案例58——热门游戏数据分析及其可视化

Python数据分析案例58——热门游戏数据分析及其可视化

案例背景

有哪个男生不喜欢玩游戏呢?就算上了班儿也要研究一下游戏以及热门的游戏。正好这里有个热门的游戏数据集,全球热门游戏数据集来做一下一些可视化的分析。


数据介绍

该文件包含一个数据集,详细说明了多个平台上的各种流行游戏。每个条目都包含重要信息,例如游戏名称、类型、平台、发布年份和用户评级。该数据集的结构便于分析和比较不同游戏,提供对游戏趋势和用户偏好的洞察。它采用CSV文件格式,以实现可访问性和易用性,使其适用于游戏行业的数据分析、可视化和研究目的。

热门的什么英雄联盟元神。还有国外的塞尔达,apex什么的都有。

需要这个数据集和这篇文章的全部代码文件的同学可以参考:游戏数据


代码实现

导入数据分析常用的包。展示数据的前五行

  1. # Import necessary libraries
  2. import pandas as pd
  3. import matplotlib.pyplot as plt
  4. import seaborn as sns
  5. # Load the dataset
  6. df_games = pd.read_csv('games_dataset.csv')
  7. # Display the first few rows and the columns of the dataset
  8. df_games.head()

简单的描述性统计

  1. # Display summary statistics of the dataset
  2. df_games.describe(include='all')

查看数据是否有缺失值

  1. # Check for missing values
  2. df_games.isnull().sum()

查看数据的类别

  1. # Remove any rows with missing values
  2. df_games_clean = df_games.dropna()
  3. # Ensure 'Release Year' is an integer
  4. df_games_clean['Release Year'] = df_games_clean['Release Year'].astype(int)
  5. # Check data types
  6. df_games_clean.dtypes


可视化分析

游戏数量柱状图

展示数据里面前10数量的游戏的中文名称及他们的数量的微型柱状图。

  1. games_dict = {
  2. "The Legend of Zelda: Breath of the Wild": "塞尔达传说:荒野之息",
  3. "Genshin Impact": "原神",
  4. "The Sims 4": "模拟人生4",
  5. "Resident Evil Village": "生化危机村庄",
  6. "Hades": "哈迪斯",
  7. "Minecraft Dungeons": "我的世界:地下城",
  8. "Sekiro: Shadows Die Twice": "只狼:影逝二度",
  9. "Horizon Zero Dawn": "地平线:黎明时分",
  10. "Sea of Thieves": "盗贼之海",
  11. "Apex Legends": " apex 英雄"
  12. }
  13. df_games['Game Name'].value_counts().rename_axis('Game_Name').reset_index().iloc[:10,:]\
  14. .assign(中文名称=lambda x:x['Game_Name'].map(games_dict)).set_axis(['Game Name','count','中文名称'],axis=1).style.bar()

可以看到这个游戏数据里面记录比较多的是塞尔达,荒野之息,这就是原神模拟人生等游戏。


整体用户评分直方图

  1. # Plot the distribution of user ratings
  2. plt.figure(figsize=(10, 6))
  3. sns.histplot(df_games_clean['User Rating'], bins=20, color='blue', edgecolor='black', kde=True)
  4. plt.title('Distribution of User Ratings')
  5. plt.xlabel('User Rating')
  6. plt.ylabel('Frequency')
  7. plt.grid(axis='y', linestyle='--', alpha=0.7)
  8. plt.show()

可以看到用户的评分较为均匀,从0分到十分,没有什么很突出的位置。说明游戏的评分没有很集中的区间。

游戏类型数量柱状图

  1. # Plot the distribution of genres
  2. plt.figure(figsize=(12, 8))
  3. sns.countplot(data=df_games_clean, y='Genre', order=df_games_clean['Genre'].value_counts().index, palette='viridis')
  4. plt.title('Genre Distribution')
  5. plt.xlabel('Count')
  6. plt.ylabel('Genre')
  7. plt.grid(axis='x', linestyle='--', alpha=0.7)
  8. plt.show()


可以看到运动类的游戏排第一,后面其次都是各种类型的游戏。但是他们的数量差别都不会很大,说明各种游戏都是有自己的市场的。

不同游戏平台数量直方图

  1. # Plot the distribution of platforms
  2. plt.figure(figsize=(12, 8))
  3. sns.countplot(data=df_games_clean, y='Platform', order=df_games_clean['Platform'].value_counts().index, palette='magma')
  4. plt.title('Platform Distribution')
  5. plt.xlabel('Count')
  6. plt.ylabel('Platform')
  7. plt.grid(axis='x', linestyle='--', alpha=0.7)
  8. plt.show()

PC端的游戏数量还是第一位,及个人电脑端,后面才是Xbox ,switch,以及移动端的各种。PS最少。

不同年份发行游戏数量的直方图

  1. # Plot the distribution of release years
  2. plt.figure(figsize=(12, 8))
  3. sns.histplot(df_games_clean['Release Year'], bins=20, color='green', edgecolor='black', kde=True)
  4. plt.title('Distribution of Release Years')
  5. plt.xlabel('Release Year')
  6. plt.ylabel('Frequency')
  7. plt.grid(axis='y', linestyle='--', alpha=0.7)
  8. plt.show()

我们可以看到大概在2000年,2007年,2015年以及2022年发行的游戏数量较多,其他年份都较为平均。


不同发行年份不同类型游戏用户评分散点图

  1. # Scatter plot of User Rating vs Release Year
  2. plt.figure(figsize=(12, 8))
  3. sns.scatterplot(data=df_games_clean, x='Release Year', y='User Rating', hue='Genre', palette='Set1', alpha=0.7)
  4. plt.title('User Rating vs Release Year')
  5. plt.xlabel('Release Year')
  6. plt.ylabel('User Rating')
  7. plt.grid(True)
  8. plt.show()

可以看到不同年份的,不同类型的数据,他们的用户得分德还是较为均匀,不同类别之间这个图不好对比。很多年份数据分布很集中,都是很均匀,从0~10分的游戏都有。说明每一年都有很多好游戏和坏游戏,没有哪一年的游戏。质量特别高,也没有哪一年的游戏质量平均特别差。


不同游戏类型数量比例柱饼图

  1. # Pie chart of Genre Distribution
  2. plt.figure(figsize=(10, 10))
  3. genre_counts = df_games_clean['Genre'].value_counts()
  4. plt.pie(genre_counts, labels=genre_counts.index, autopct='%1.1f%%', colors=sns.color_palette('pastel'), startangle=140)
  5. plt.title('Genre Distribution')
  6. plt.show()

和前面的结论一致,可以看到游戏类型最多的是运动类。而其他的类别分布都没有差太远的比例,说明各种游戏都有各自的市场,没有哪一种游戏是极少或者极多的。

不同游戏平台数量比例柱饼图

  1. # Pie chart of Platform Distribution
  2. plt.figure(figsize=(10, 10))
  3. platform_counts = df_games_clean['Platform'].value_counts()
  4. plt.pie(platform_counts, labels=platform_counts.index, autopct='%1.1f%%', colors=sns.color_palette('pastel'), startangle=140)
  5. plt.title('Platform Distribution')
  6. plt.show()

和前面结论一致,PC端的数量占比最高,说明目前大家还是用电脑玩游戏的最多,当然其他的平台差距也不大,各个平台都有各自的用户。


不同类别的游戏用户评分箱线图

  1. # Boxplot of User Ratings by Genre
  2. plt.figure(figsize=(14, 8))
  3. sns.boxplot(data=df_games_clean, x='Genre', y='User Rating', palette='coolwarm')
  4. plt.title('User Ratings by Genre')
  5. plt.xlabel('Genre')
  6. plt.ylabel('User Rating')
  7. plt.xticks(rotation=45)
  8. plt.grid(axis='y', linestyle='--', alpha=0.7)
  9. plt.show()

可以从均值的角度来看,发现生存类的游戏评分均值最高。其次是恐怖游戏和角色扮演类动作类游戏。但是他们整体来说均值差异都不大。


不同平台用户评分箱线图

  1. # Boxplot of User Ratings by Platform
  2. plt.figure(figsize=(14, 8))
  3. sns.boxplot(data=df_games_clean, x='Platform', y='User Rating', palette='coolwarm')
  4. plt.title('User Ratings by Platform')
  5. plt.xlabel('Platform')
  6. plt.ylabel('User Rating')
  7. plt.xticks(rotation=45)
  8. plt.grid(axis='y', linestyle='--', alpha=0.7)
  9. plt.show()

我们可以看到PC端的用户评分均值最高,其次是其他4个都差不多。并且大家的分布都很均匀,差距都不是很大。


不同类型游戏用户评分均值柱状图

  1. # Calculate average user rating by genre
  2. avg_rating_by_genre = df_games_clean.groupby('Genre')['User Rating'].mean().sort_values()
  3. # Plot average user ratings by genre
  4. plt.figure(figsize=(12, 8))
  5. avg_rating_by_genre.plot(kind='barh', color='purple', edgecolor='black')
  6. plt.title('Average User Ratings by Genre')
  7. plt.xlabel('Average User Rating')
  8. plt.ylabel('Genre')
  9. plt.grid(axis='x', linestyle='--', alpha=0.7)
  10. plt.show()

和前面的一致,生存类,恐怖类,模拟经营类的动作类运动类。评分前五,其他的游戏评分略低,但是也还好。差距都不是很大。

不同平台用户评分均值柱状图

  1. # Calculate average user rating by platform
  2. avg_rating_by_platform = df_games_clean.groupby('Platform')['User Rating'].mean().sort_values()
  3. # Plot average user ratings by platform
  4. plt.figure(figsize=(12, 8))
  5. avg_rating_by_platform.plot(kind='barh', color='orange', edgecolor='black')
  6. plt.title('Average User Ratings by Platform')
  7. plt.xlabel('Average User Rating')
  8. plt.ylabel('Platform')
  9. plt.grid(axis='x', linestyle='--', alpha=0.7)
  10. plt.show()

和前面结论一致pc端电脑端的用户评分均值最高,其他的都差不多。


发行年份和用户评分的相关性热力图

  1. # Compute correlation matrix
  2. corr_matrix = df_games_clean[['Release Year', 'User Rating']].corr()
  3. # Plot correlation heatmap
  4. plt.figure(figsize=(8, 6))
  5. sns.heatmap(corr_matrix, annot=True, cmap='coolwarm', fmt='.2f', linewidths=0.5)
  6. plt.title('Correlation Heatmap')
  7. plt.show()

可以看到和散点图的结论一样,发行年份和评分没有相关性。就是零。

游戏类别和游戏平台的评分相关系数热力图

  1. # Pivot table for heatmap
  2. pivot_table = df_games_clean.pivot_table(values='User Rating', index='Genre', columns='Platform', aggfunc='mean')
  3. # Plot heatmap
  4. plt.figure(figsize=(14, 8))
  5. sns.heatmap(pivot_table, cmap='YlGnBu', annot=True, fmt='.1f', linewidths=0.5)
  6. plt.title('Heatmap of User Ratings by Genre and Platform')
  7. plt.xlabel('Platform')
  8. plt.ylabel('Genre')
  9. plt.show()

我们可以看到某些平台的某些类型的游戏评分明显要高于其他,例如电脑端的策略类和生存类,还有格斗类以及射击类明显要优于其他平台,也要优于电脑端的其他的类型的游戏的评分的均值。


  1. # Plot the count of games by Release Year and Genre
  2. plt.figure(figsize=(14, 8))
  3. sns.countplot(data=df_games_clean, x='Release Year', hue='Genre', palette='Set2')
  4. plt.title('Distribution of Games by Release Year and Genre')
  5. plt.xlabel('Release Year')
  6. plt.ylabel('Number of Games')
  7. plt.xticks(rotation=45)
  8. plt.grid(axis='y', linestyle='--', alpha=0.7)
  9. plt.show()

这个是不同年份发行不同类别的游戏,难以看出明显的趋势,只能大概知道2006年发行的Puzzle这个类型的游戏比较多。


 不同年份发行游戏数量折线图

  1. # Plot trends in the number of releases per year
  2. plt.figure(figsize=(14, 8))
  3. release_year_trends = df_games_clean['Release Year'].value_counts().sort_index()
  4. release_year_trends.plot(kind='line', marker='o', color='tomato')
  5. plt.title('Trends in Number of Game Releases Per Year')
  6. plt.xlabel('Release Year')
  7. plt.ylabel('Number of Releases')
  8. plt.grid(True)
  9. plt.show()

可以清楚的看到每个年份发行的游戏的数量的一个变化趋势。


 不同类型的游戏的用户评分的核密度图

  1. # Kernel Density Estimate (KDE) plot of user ratings by genre
  2. plt.figure(figsize=(14, 8))
  3. sns.kdeplot(data=df_games_clean, x='User Rating', hue='Genre', common_norm=False, palette='tab10')
  4. plt.title('Density Plot of User Ratings by Genre')
  5. plt.xlabel('User Rating')
  6. plt.ylabel('Density')
  7. plt.grid(True)
  8. plt.show()

其实和前面结论一样,不同类型的这些用户评分分布都挺相近的,可能有的稍微高一点点,但是也不会高很多。并且分布都是两端比较少,也就是说很好或者很差的游戏比较少,大多数游戏都集中在中间的2-9分的区间。


不同平台发布不同类型游戏的数量热力图

  1. # Create a DataFrame for analysis of platform and genre pairs
  2. platform_genre_counts = df_games_clean.groupby(['Platform', 'Genre']).size().unstack().fillna(0)
  3. # Plot
  4. plt.figure(figsize=(16, 10))
  5. sns.heatmap(platform_genre_counts, cmap='viridis', annot=True, fmt='g', linewidths=0.5)
  6. plt.title('Platform and Genre Pair Analysis')
  7. plt.xlabel('Genre')
  8. plt.ylabel('Platform')
  9. plt.show()

可以从图中清楚的看到每个平台发布每个不同类型的游戏的数量。例如运动类在移动端和电脑端发布的数量是最多的,其次就是xbox上的角色扮演类的游戏数量比较多。

不同发布年份用户评分的箱线图

  1. # Box plot of user ratings by release year
  2. plt.figure(figsize=(14, 8))
  3. sns.boxplot(data=df_games_clean, x='Release Year', y='User Rating', palette='coolwarm')
  4. plt.title('User Ratings by Release Year')
  5. plt.xlabel('Release Year')
  6. plt.ylabel('User Rating')
  7. plt.xticks(rotation=45)
  8. plt.grid(axis='y', linestyle='--', alpha=0.7)
  9. plt.show()

可以看到大概在2004年和2016年发布的游戏评分均值较高,2007年和2023年发布的游戏评分均值较低,但整体变化波动幅度不是很大。


 不同发布年份用户评分均值的折线图

  1. # Line plot of average user ratings over time (by Release Year)
  2. plt.figure(figsize=(14, 8))
  3. avg_rating_over_time = df_games_clean.groupby('Release Year')['User Rating'].mean()
  4. avg_rating_over_time.plot(kind='line', marker='o', color='skyblue')
  5. plt.title('Average User Ratings Over Time')
  6. plt.xlabel('Release Year')
  7. plt.ylabel('Average User Rating')
  8. plt.grid(True)
  9. plt.show()

不同年份用户评分均值折线图。

可以清楚地看到不同年份的游戏数据评分均值分布。和前面的箱线图得到的结论也是类似的。


后记

只是简单的画了一些柱状图,折线图。直方图密度图,其实还有很多箱线图。概率密度图,小提琴图,点图以及散点,面积,气泡,雷达等图都可以画,但是目前简单的分析就这个样子。

其实还可以单独抽出各种游戏,例如原神整体的每个年份的一个评分的变化分布之类的,但是我没有很偏好的单独的游戏,所以就没有做,大家有兴趣可以自己做一下。

数据和全部代码文件获取参考:游戏数据


创作不易,看官觉得写得还不错的话点个关注和赞吧,本人会持续更新python数据分析领域的代码文章~(需要定制类似的代码可私信)

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/代码探险家/article/detail/933346
推荐阅读
相关标签
  

闽ICP备14008679号