Reputation: 57
I want to plot this dataframe like a time series, a line for every country that every year increases or decreases according to 'count'. How can i do this?
country count
Year
2005 Australia 2
2005 Austria 1
2005 Belgium 0
2005 Canada 4
2005 China 0
2006 Australia 3
2006 Austria 0
2006 Belgium 1
2006 Canada 5
2006 China 2
2007 Australia 5
2007 Austria 1
2007 Belgium 2
2007 Canada 6
2007 China 3
Upvotes: 4
Views: 140
Reputation: 865
A pure pandas solution would be using pivot and plot
df = pd.DataFrame({'Year':[2005,2005,2005,2005,2005,
2006,2006,2006,2006,2006,
2007,2007,2007,2007,2007],
'country' : ['Australia', 'Austria','Belgium', 'Canada', 'China']*3,
'count':[2,1,0,4,0,3,0,1,5,2,5,1,2,6,3]})
df.pivot(index='Year', columns='country', values='count').plot(xticks=df['Year'].unique())
Resulting in:
Upvotes: 2
Reputation: 14064
Using matplotlib
(and seaborn
styling):
Setup
import pandas as pd
data = {'Year': {0: 2005, 1: 2005, 2: 2005, 3: 2005, 4: 2005, 5: 2006,
6: 2006, 7: 2006, 8: 2006, 9: 2006, 10: 2007, 11: 2007,
12: 2007, 13: 2007, 14: 2007},
'country': {0: 'Australia', 1: 'Austria', 2: 'Belgium', 3: 'Canada',
4: 'China', 5: 'Australia', 6: 'Austria', 7: 'Belgium',
8: 'Canada', 9: 'China', 10: 'Australia', 11: 'Austria',
12: 'Belgium', 13: 'Canada', 14: 'China'},
'count': {0: 2, 1: 1, 2: 0, 3: 4, 4: 0, 5: 3, 6: 0, 7: 1, 8: 5, 9: 2,
10: 5, 11: 1, 12: 2, 13: 6, 14: 3}}
df = pd.DataFrame(data)
Plot
import matplotlib.pyplot as plt
import seaborn as sns
sns.set_style("dark")
df_pivot = df.pivot(index='Year', columns='country', values='count')
fig, ax = plt.subplots(figsize=(16,10))
ax.plot(df_pivot)
ax.grid()
ax.set_ylabel('Count')
ax.set_xlabel('Years')
ax.set_xticks(df_pivot.index.unique())
ax.legend(df_pivot.columns, title='Country count', fontsize=16)
plt.show()
Result:
As you can see from the answer by Nuri Taş, seaborn
can do a lot of this "work" for you. But it is useful to understand what is actually being done.
Upvotes: 3
Reputation: 3260
You can use pd.pivot_table and df.plot for this:
df.pivot_table(index='Year', columns='country', values='count').plot(xticks=df.Year.unique())
Will return
Upvotes: 5
Reputation: 3845
You can use seaborn.lineplot
:
import seaborn as sns
df.Year = pd.to_datetime(df.Year)
sns.set(rc={'figure.figsize':(12, 8)}) # changed the figure size to avoid overlapping
sns.lineplot(data=df, x=df['Year'].dt.strftime('%Y'), # show only years with strftime
y=df['count'], hue='country')
Upvotes: 3