Plat_Genre = pd.crosstab(data.Platform,data.Genre) Plat_Genre_sum = Plat_Genre.sum(axis=1).sort_values(ascending = False) Plat_Global_Sales = data.groupby('Platform')['Global_Sales'].sum().sort_values(ascending = False) Genre_Global_Sales = data.groupby('Genre')['Global_Sales'].sum().sort_values(ascending = False) f,ax = plt.subplots(1,3,figsize=(25,8),dpi=100) sns.barplot(Plat_Genre_sum.values,Plat_Genre_sum.index,ax=ax[0]) ax[0].set_title('Platform_Genre') sns.barplot(Plat_Global_Sales.values,Plat_Global_Sales.index,ax=ax[1]) ax[1].set_title('Platform_Global_Sales') sns.barplot(Genre_Global_Sales.values,Genre_Global_Sales.index,ax=ax[2]) ax[2].set_title('Genre_Global_Sales') plt.show()
时间: 2024-02-05 11:04:50 浏览: 31
这段代码使用了 `matplotlib` 和 `seaborn` 库来创建一个包含三个子图的图表,用于可视化数据集中的平台、类型和全球销售额的关系。
首先,通过 `pd.crosstab(data.Platform, data.Genre)` 创建了一个交叉表 `Plat_Genre`,用于统计不同平台和不同类型的游戏数量。
然后,通过 `Plat_Genre.sum(axis=1).sort_values(ascending=False)` 计算了每个平台上游戏总数,并按降序排序,结果存储在变量 `Plat_Genre_sum` 中。
接下来,通过 `data.groupby('Platform')['Global_Sales'].sum().sort_values(ascending=False)` 对平台进行分组并计算全球销售额总和,再按降序排序,结果存储在变量 `Plat_Global_Sales` 中。
最后,通过 `data.groupby('Genre')['Global_Sales'].sum().sort_values(ascending=False)` 对类型进行分组并计算全球销售额总和,再按降序排序,结果存储在变量 `Genre_Global_Sales` 中。
接下来的代码使用 `plt.subplots(1, 3, figsize=(25, 8), dpi=100)` 创建了一个包含三个子图的图表,并设置了图表的大小和分辨率。
然后,使用 `sns.barplot()` 分别绘制了三个子图,分别是平台与类型关系、平台与全球销售额关系、类型与全球销售额关系。通过 `Plat_Genre_sum.values` 和 `Plat_Genre_sum.index`,`Plat_Global_Sales.values` 和 `Plat_Global_Sales.index`,`Genre_Global_Sales.values` 和 `Genre_Global_Sales.index` 将数值和对应的标签传递给 `sns.barplot()`。
最后,使用 `ax.set_title()` 设置了每个子图的标题。
最后一行的 `plt.show()` 用于显示图表。