问题
I'm struggling to (re)create a seaborn plot with pandas or matplotlib.
DataFrame:
wage = pd.melt(pd.read_html('https://en.wikipedia.org/wiki/List_of_countries_by_average_wage')[8].iloc[:5],
id_vars=['Country'],var_name='Year', value_name='Wage')
print(wage.sample(n=7))
Result:
Country Year Wage
29 Netherlands 2013 52808
38 United States 2015 60692
4 Netherlands 2000 47596
9 Netherlands 2005 49939
23 United States 2012 58669
46 Switzerland 2017 62283
12 Iceland 2010 44558
The plot using seaborn is easy:
fig, ax = plt.subplots(figsize=(15, 7))
sns.lineplot(x='Year', y='Wage', hue='Country', linewidth=3, data=wage, ax=ax)
ax.set_title('Development of average annual wages 2000–2017 (US$ PPP)', fontsize=16)
plt.show()
But I would like to create the same plot with pandas using wage.plot()
or with matplotlib
. Any suggestions how to do that?
回答1:
You can loop over the wage['Country']
:
fig, ax = plt.subplots()
for c, d in wage.groupby('Country'):
ax.plot(d.Year, d.Wage, label=c)
plt.legend()
plt.show()
which gives:
来源:https://stackoverflow.com/questions/57296334/create-seaborn-plot-with-pandas-of-matplotlib