Search code examples
pandasmatplotlibseabornline-plot

How to plot lineplot with several lines as countries from dataset


Encountered a problem whilst plotting from GDP dataset:

enter image description here

As I trying to plot, I cannot figure out how to take more than 1 year:

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

output:

enter image description here

How to plot all years on X, amount on Y and lines as each country ? How to modify Y stick to shown whole digits, not only 1-2-3-4-5-6 and lell


Solution

  • You need to transform your dataframe to "long form" format, then pass the relevant column names to 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