如何从数据集中绘制多条线作为国家的线图

时间:2020-09-01 12:02:38

标签: pandas matplotlib seaborn line-plot

从GDP数据集绘图时遇到问题:

enter image description here

当我尝试绘制时,我不知道怎么花超过一年的时间:

plt.figure(figsize=(14,6))
gdp = sns.lineplot(x=df_gdp['Country Name'], y=df_gdp['1995'], marker='o', color='mediumvioletred', sort=False)
for item in gdp.get_xticklabels():
    item.set_rotation(45)
plt.xticks(ha='right',fontweight='light',fontsize='large')

输出:

enter image description here

如何在每个国家的X上绘制全年,在Y上绘制金额并在每个国家/地区绘制线条? 如何将Y棒修改为显示的整数,不仅 1-2-3-4-5-6 lell

1 个答案:

答案 0 :(得分:0)

您需要将数据框转换为“长格式”格式,然后将相关的列名传递给lineplot

df2 = df.melt(id_vars=['Country Name'], var_name='year', value_name='GDP')
sns.lineplot(x='year', y='GDP', hue='Country Name', data=df2)

enter image description here