简体   繁体   中英

create seaborn plot with pandas of matplotlib

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?

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:

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM