[英]How to plot a time series with this dataframe?
I want to plot this dataframe like a time series, a line for every country that every year increases or decreases according to 'count'.我想 plot 这个 dataframe 就像一个时间序列,每个国家的一条线,每年根据“计数”增加或减少。 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
You can use pd.pivot_table and df.plot for this:您可以为此使用pd.pivot_table和df.plot :
df.pivot_table(index='Year', columns='country', values='count').plot(xticks=df.Year.unique())
Will return将返回
You can use seaborn.lineplot
:您可以使用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')
Using matplotlib
(and seaborn
styling):使用matplotlib
(和seaborn
样式):
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 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.正如您从 Nuri Taş 的回答中看到的那样, seaborn
可以为您做很多这样的“工作”。 But it is useful to understand what is actually being done.但了解实际在做什么是有用的。
A pure pandas solution would be using pivot and plot纯 pandas 解决方案将使用pivot和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:导致:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.