python - 使用 matplotlib 的 pandas 创建 seaborn 绘图

标签 python pandas matplotlib seaborn

我正在努力使用 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()

enter image description here

但我想使用 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()

给出:

enter image description here

关于python - 使用 matplotlib 的 pandas 创建 seaborn 绘图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57296334/

相关文章:

python - 如何在 python 中使用 selenium 提交此表单?

python - 如何在使用 groupby() 时忽略 pandas 数据框中具有唯一索引的几行?

python - pandas.Series 中的加权插值

python - 在一张图中绘制两条曲线时遇到错误(python-pandas-matplotlib)

python - 如何使用 matplotlib 在形状上画一个洞

python - pip install matplotlib 在使用 Homebrew 软件的特立独行者上失败

python - scipy kstest 在不同范围内不一致

python - python Anaconda 中的 mayavi

python - python pre-commit 工具是否可以将子目录视为项目的基础?

python - 如何分组应用聚合回到 Python Pandas 中的数据框?