Reputation: 41
I'm using IGN review dataset from kaggle, and i'm trying to get a frequency plot by each nintendo platform x week day of the given launch date, here is the code
import pandas as pd
df = pd.read_csv("ign.csv")
datetime_df = pd.DataFrame({'year': df["release_year"],
'month': df["release_month"],
'day': df["release_day"]})
df["date"] = pd.to_datetime(datetime_df)
df["week_day"] = df["date"].apply(lambda x : x.weekday_name)
nintendo = ['Wii','Nintendo DS','Nintendo 3DS','Nintendo DS',
'Game Boy', 'Game Boy Color','Nintendo 64DD','Game Boy Advance',
'New Nintendo 3DS','GameCube','Nintendo DSi','Super NES']
base_nintendo = df[df["platform"].isin(nintendo)]
data = base_nintendo.groupby(["platform","week_day"]).size()
data =data.unstack().fillna(0).stack()
data
with the output:
platform week_day
Game Boy Friday 5.0
Monday 5.0
Saturday 0.0
Sunday 0.0
Thursday 0.0
Tuesday 4.0
Wednesday 8.0
Game Boy Advance Friday 131.0
Monday 109.0
Saturday 0.0
Sunday 1.0
Thursday 153.0
Tuesday 123.0
Wednesday 106.0
Game Boy Color Friday 89.0
Monday 43.0
Saturday 1.0
Sunday 1.0
Thursday 55.0
Tuesday 78.0
Wednesday 89.0
GameCube Friday 99.0
Monday 100.0
Saturday 3.0
Sunday 0.0
Thursday 83.0
Tuesday 124.0
Wednesday 100.0
I've tried doing:
data.groupby("platform").plot("barh")
but it only gives me the last platform (wii):
Upvotes: 0
Views: 374
Reputation: 54330
Notice that above the plot you get one line for each of your groups such as Super NES ....
? Those are the matplotlib.AxesSubplot
object to your other plots.
groupby.plot
actually returns a matplotlib.AxesSubplot
object for each of your groups. On the other hand, ipython notebook is only showing your last plot.
The solution is thus: change your data.groupby("platform").plot("barh")
to my_axes = data.groupby("platform").plot("barh")
and then work on them one by one such as
for ax in my_axes:
ax.savefig(filename)
Alternative you can do this:
gp = data.groupby("platform")
f, axes = plt.subplots(5, 5) # or any other large enough subplot grid
for k, ax in zip(gp.groups, axes.ravel()):
gp.get_group(k).plot('barh', ax=ax)
Upvotes: 1
Reputation: 715
One solution would be to use seaborn
and plot barh
.
data = data.unstack().fillna(0).stack()
data = data.reset_index().rename(columns={0:'value'})
import seaborn as sns
import matplotlib.pyplot as plt
fig, ax = plt.subplots(figsize=(10,7))
sns.barplot(y='platform',x='value', hue='week_day', data=data, orient='h')
plt.show()
Upvotes: 1