我正在努力使用 pandas 或 matplotlib(重新)创建一个 seaborn 绘图。
数据框:
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))
结果:
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
使用seaborn的绘图很简单:
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()
但我想使用 wage.plot()
或使用 matplotlib
与 pandas 创建相同的绘图。有什么建议吗?
最佳答案
您可以循环遍历工资['Country']
:
fig, ax = plt.subplots()
for c, d in wage.groupby('Country'):
ax.plot(d.Year, d.Wage, label=c)
plt.legend()
plt.show()
给出:
关于python - 使用 matplotlib 的 pandas 创建 seaborn 绘图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57296334/