There is a negative correlation between horsepower and miles per gallon.
mtcars %>%
ggplot(aes(x = hp, y = mpg)) +
geom_point()
mtcars %>%
glimpse()
## Rows: 32
## Columns: 11
## $ mpg <dbl> 21.0, 21.0, 22.8, 21.4, 18.7, 18.1, 14.3, 24.4, 22.8, 19.2, 17.8,…
## $ cyl <dbl> 6, 6, 4, 6, 8, 6, 8, 4, 4, 6, 6, 8, 8, 8, 8, 8, 8, 4, 4, 4, 4, 8,…
## $ disp <dbl> 160.0, 160.0, 108.0, 258.0, 360.0, 225.0, 360.0, 146.7, 140.8, 16…
## $ hp <dbl> 110, 110, 93, 110, 175, 105, 245, 62, 95, 123, 123, 180, 180, 180…
## $ drat <dbl> 3.90, 3.90, 3.85, 3.08, 3.15, 2.76, 3.21, 3.69, 3.92, 3.92, 3.92,…
## $ wt <dbl> 2.620, 2.875, 2.320, 3.215, 3.440, 3.460, 3.570, 3.190, 3.150, 3.…
## $ qsec <dbl> 16.46, 17.02, 18.61, 19.44, 17.02, 20.22, 15.84, 20.00, 22.90, 18…
## $ vs <dbl> 0, 0, 1, 1, 0, 1, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0,…
## $ am <dbl> 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0,…
## $ gear <dbl> 4, 4, 4, 3, 3, 3, 3, 4, 4, 4, 4, 3, 3, 3, 3, 3, 3, 4, 4, 4, 3, 3,…
## $ carb <dbl> 4, 4, 1, 1, 2, 1, 4, 2, 2, 4, 4, 3, 3, 3, 4, 4, 4, 1, 2, 1, 1, 2,…
plt.boxplot(selected_variables.values, showfliers=False, labels=selected_variables.columns)
## {'whiskers': [<matplotlib.lines.Line2D object at 0x00000188822CD120>, <matplotlib.lines.Line2D object at 0x00000188822CD3C0>, <matplotlib.lines.Line2D object at 0x00000188822CE0E0>, <matplotlib.lines.Line2D object at 0x00000188822CE380>, <matplotlib.lines.Line2D object at 0x00000188822CF0A0>, <matplotlib.lines.Line2D object at 0x00000188822CF340>, <matplotlib.lines.Line2D object at 0x00000188825900A0>, <matplotlib.lines.Line2D object at 0x0000018882590340>, <matplotlib.lines.Line2D object at 0x0000018882591060>, <matplotlib.lines.Line2D object at 0x0000018882591300>], 'caps': [<matplotlib.lines.Line2D object at 0x00000188822CD660>, <matplotlib.lines.Line2D object at 0x00000188822CD900>, <matplotlib.lines.Line2D object at 0x00000188822CE620>, <matplotlib.lines.Line2D object at 0x00000188822CE8C0>, <matplotlib.lines.Line2D object at 0x00000188822CF5E0>, <matplotlib.lines.Line2D object at 0x00000188822CF880>, <matplotlib.lines.Line2D object at 0x00000188825905E0>, <matplotlib.lines.Line2D object at 0x0000018882590880>, <matplotlib.lines.Line2D object at 0x00000188825915A0>, <matplotlib.lines.Line2D object at 0x0000018882591840>], 'boxes': [<matplotlib.lines.Line2D object at 0x00000188822CCE80>, <matplotlib.lines.Line2D object at 0x00000188822CDE40>, <matplotlib.lines.Line2D object at 0x00000188822CEE00>, <matplotlib.lines.Line2D object at 0x00000188822CFDC0>, <matplotlib.lines.Line2D object at 0x0000018882590DC0>], 'medians': [<matplotlib.lines.Line2D object at 0x00000188822CDBA0>, <matplotlib.lines.Line2D object at 0x00000188822CEB60>, <matplotlib.lines.Line2D object at 0x00000188822CFB20>, <matplotlib.lines.Line2D object at 0x0000018882590B20>, <matplotlib.lines.Line2D object at 0x0000018882591AE0>], 'fliers': [], 'means': []}
plt.title('Figure 1: The sales of game in different regions', fontsize=12)
plt.show()
fig.show()
go.Figure(data=[
go.Scatter(
x=df.groupby(df['Year_of_Release'].sort_values()).sum().drop(['Year_of_Release'],axis=1).reset_index()['Year_of_Release'],
y=df.groupby(df['Year_of_Release'].sort_values()).sum().drop(['Year_of_Release'],axis=1).reset_index()['NA_Sales'],
mode='lines+markers',
name='North America Sales',
marker = dict(size=8),
line=dict(color = '#FA8072',width=2.5),
hovertemplate = '<i>Year: %{x}</i>'
'<br><i>Sales: %{y} </i>'),
go.Scatter(
x=df.groupby(df['Year_of_Release'].sort_values()).sum().drop(['Year_of_Release'],axis=1).reset_index()['Year_of_Release'],
y=df.groupby(df['Year_of_Release'].sort_values()).sum().drop(['Year_of_Release'],axis=1).reset_index()['EU_Sales'],
mode='lines+markers',
name='Europe Sales',
marker = dict(size=8),
line=dict(color = '#6495ED',width=2.5),
hovertemplate = 'Year: %{x}'
'<br><i>Sales: %{y} </i>'),
go.Scatter(
x=df.groupby(df['Year_of_Release'].sort_values()).sum().drop(['Year_of_Release'],axis=1).reset_index()['Year_of_Release'],
y=df.groupby(df['Year_of_Release'].sort_values()).sum().drop(['Year_of_Release'],axis=1).reset_index()['JP_Sales'],
mode='lines+markers',
name='Japan Sales',
marker = dict(size=8),
line=dict(color = 'yellowgreen',width=2.5),
hovertemplate = '<i>Year: %{x}</i>'
'<br><i>Sales: %{y} </i>'),
go.Scatter(
x=df.groupby(df['Year_of_Release'].sort_values()).sum().drop(['Year_of_Release'],axis=1).reset_index()['Year_of_Release'],
y=df.groupby(df['Year_of_Release'].sort_values()).sum().drop(['Year_of_Release'],axis=1).reset_index()['Other_Sales'],
mode='lines+markers',
name='Other Country Sales',
marker = dict(size=8),
line=dict(color = '#DAA520',width=2.5),
hovertemplate = '<i>Year: %{x}</i>'
'<br><i>Sales: %{y} </i>')
],layout=dict(legend=dict(x=-0.04, y=1.09,font=dict(size=10)),legend_orientation="h",title="Sales of Games in Different Countries over the Years",
xaxis=dict(tickmode = 'linear',tickangle=-90,tickfont=dict(size=10),title="Year of Release",tickwidth=5,ticklen=8,zeroline=False,gridcolor="white"),
yaxis=dict(title="Gross Sales in Different Countries",gridcolor="#DCDCDC")
,paper_bgcolor='white',plot_bgcolor='white'))
sales = [col for col in df.columns if col.endswith('Sales') and not col.startswith('Total')]
genre_sales = df.groupby(['Genre'])[sales].sum()
cmap = plt.get_cmap('Greys')
colors = list(cmap(np.linspace(0.45, 0.85, len(genre_sales))))
colors = sns.color_palette("Spectral")
explode = [0.1] + [0] * 7
genre_global_sales = (df.groupby(['Genre'])['Global_Sales'].sum()
.sort_values(ascending=False))
fig, ax = plt.subplots(figsize=(6, 6), facecolor='white')
ax.pie(np.append(genre_global_sales.values[:7],
sum(genre_global_sales.values)-sum(genre_global_sales.values[:7])),
labels=np.append(genre_global_sales.index[:7], 'Other'),
autopct='%.1f%%', colors=colors, explode=explode,
pctdistance=0.8,
textprops={'fontsize': 13}
)
## ([<matplotlib.patches.Wedge object at 0x000001888A62D510>, <matplotlib.patches.Wedge object at 0x000001888A62E7A0>, <matplotlib.patches.Wedge object at 0x000001888A62D960>, <matplotlib.patches.Wedge object at 0x000001888A62D600>, <matplotlib.patches.Wedge object at 0x000001888A62EC20>, <matplotlib.patches.Wedge object at 0x000001888A62F2B0>, <matplotlib.patches.Wedge object at 0x000001888A62DAE0>, <matplotlib.patches.Wedge object at 0x000001888A62FFD0>], [Text(0.9802498947355807, 0.6921778267691643, 'Action'), Text(-0.1405131261083664, 1.0909885706968954, 'Sports'), Text(-0.9062886504124924, 0.6234106849689883, 'Shooter'), Text(-1.0947692167918215, -0.10714645101459054, 'Role-Playing'), Text(-0.8281114404628523, -0.7240382877821724, 'Platform'), Text(-0.30154656535898705, -1.057860892991228, 'Misc'), Text(0.2849493520616443, -1.062451818559152, 'Racing'), Text(0.9513084093934006, -0.5522791958940679, 'Other')], [Text(0.7351874210516854, 0.5191333700768732, '19.6%'), Text(-0.10219136444244828, 0.7934462332341057, '14.9%'), Text(-0.6591190184818126, 0.45338958906835514, '11.8%'), Text(-0.7961957940304156, -0.07792469164697494, '10.5%'), Text(-0.6022628657911654, -0.5265733002052162, '9.3%'), Text(-0.2193065929883542, -0.7693533767208931, '9.0%'), Text(0.20723589240846857, -0.7726922316793833, '8.2%'), Text(0.6918606613770186, -0.4016575970138676, '16.7%')])
plt.title("Global Total Sales", fontsize=18)
plt.show()
stations = ['PS3', 'PS4', 'X360','XOne', 'Wii', 'WiiU', 'PC']
portable = ['DS', 'PSP', 'PSV']
all_platforms = stations + portable
past_gen = ['Wii', 'PS3', 'X360']
next_gen = ['WiiU', 'PS4', 'XOne']
main_df = df[df['Platform'].apply(lambda x: x in all_platforms) ]
main_df['portable'] = 0
## <string>:1: SettingWithCopyWarning:
##
##
## A value is trying to be set on a copy of a slice from a DataFrame.
## Try using .loc[row_indexer,col_indexer] = value instead
##
## See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy
main_df.loc[main_df['Platform'].apply(lambda x:x in [ 'DS', 'PSP', 'PSV']), 'portable'] = 1
fig = plt.figure(figsize=(15, 15))
ax = sns.boxplot(x='Platform', y='Global_Sales', data=main_df, palette='Spectral')
ax = sns.stripplot(x='Platform', y='Global_Sales', data=main_df, palette='Spectral', jitter=0.2, size=2.5)
## <string>:1: FutureWarning:
##
## Passing `palette` without assigning `hue` is deprecated.
plt.title("Boxplot platform sales")
plt.grid()
plt.show()
col_remove = ['Critic_Score','Critic_Count','User_Score','User_Count','Developer','Rating']
df_clear = df.drop(labels=col_remove, axis=1)
df_clear.info()
## <class 'pandas.core.frame.DataFrame'>
## RangeIndex: 16719 entries, 0 to 16718
## Data columns (total 10 columns):
## # Column Non-Null Count Dtype
## --- ------ -------------- -----
## 0 Name 16717 non-null object
## 1 Platform 16719 non-null object
## 2 Year_of_Release 16450 non-null float64
## 3 Genre 16717 non-null object
## 4 Publisher 16665 non-null object
## 5 NA_Sales 16719 non-null float64
## 6 EU_Sales 16719 non-null float64
## 7 JP_Sales 16719 non-null float64
## 8 Other_Sales 16719 non-null float64
## 9 Global_Sales 16719 non-null float64
## dtypes: float64(6), object(4)
## memory usage: 1.3+ MB
df_clear=df_clear.dropna(axis=0)
df_clear.head()
## Name Platform ... Other_Sales Global_Sales
## 0 Wii Sports Wii ... 8.45 82.53
## 1 Super Mario Bros. NES ... 0.77 40.24
## 2 Mario Kart Wii Wii ... 3.29 35.52
## 3 Wii Sports Resort Wii ... 2.95 32.77
## 4 Pokemon Red/Pokemon Blue GB ... 1.00 31.37
##
## [5 rows x 10 columns]
df_sales_sort = df_clear.sort_values(by=['Global_Sales'], ascending=False)
df_sales_sort_hd = df_sales_sort.head(10).sort_values(by=['Global_Sales'], ascending = True)
top10_games_gs = df_sales_sort_hd['Name'].head(10)
top10_sales_gs = df_sales_sort_hd['Global_Sales'].head(10)
colors = sns.color_palette("Spectral")
fig, ax = plt.subplots(figsize=(10, 6))
p1 = ax.barh(top10_games_gs, top10_sales_gs, color=colors)
ax.set_title('Top 10 Global Selling Video Games')
ax.set_xlabel('Sales [million units]')
ax.set_ylabel('Video game names')
ax.bar_label(p1, label_type='center')
plt.tight_layout()
plt.show()
Thà is lallalalaal