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
I'd like a thing like this:
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
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')
which gives
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With