使用 matplotlib 的 Pandas 创建 seaborn 图 [英] create seaborn plot with pandas of matplotlib

查看:56
本文介绍了使用 matplotlib 的 Pandas 创建 seaborn 图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力(重新)使用 pandas 或 matplotlib 创建一个 seaborn 图.

I'm struggling to (re)create a seaborn plot with pandas or matplotlib.

数据帧:

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进行情节很简单:

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()

但我想使用 wage.plot()matplotlib 用熊猫创建相同的图.有什么建议可以做到吗?

But I would like to create the same plot with pandas using wage.plot() or with matplotlib. Any suggestions how to do that?

推荐答案

您可以遍历 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()

给出:

这篇关于使用 matplotlib 的 Pandas 创建 seaborn 图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆