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

问题描述

从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

解决方法

您需要将数据框转换为“长格式”格式,然后将相关的列名传递给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

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...