To plot all countries but only show the top 5 countries by trade_usd in the legend, you can use the legend_params argument in the relplot function to customize the legend. Specifically, you can set legend_params=dict(legend_type="line", orientation="h"), which will create a horizontal legend with lines representing the different countries. Then, you can use the legend_params argument to filter the countries that are displayed in the legend based on their trade_usd value. Here's an example code snippet that should accomplish this:
This will create a horizontal legend with lines representing all countries, but only the top 5 countries by trade_usd will be displayed in the legend. The filter_params argument is used to filter the countries based on their trade_usd value, and the top5 value in the filter_params dictionary specifies that only the top 5 countries by trade_usd should be displayed in the legend.
Alternatively, you can also use the scale_color_manual function to customize the color palette and display only the top 5 countries in the legend. Here's an example code snippet that should accomplish this:
code
sns.relplot(total_trade_by_country_and_year, x="year", y="trade_usd",
hue="country", kind="line", palette="Set2",
legend_params=dict(legend_type="line", orientation="h",
scale_color_manual=dict(pal=palette,
limits=[0, 5],
values=[i for i, x in enumerate(total_trade_by_country_and_year['trade_usd']) if x > 5]))
This will create a horizontal legend with lines representing all