How to Create Eye-Catching Country Rankings Using Python and Matplotlib | by Oscar Leo | Aug, 2023

Matplotlib Tutorial

A beautiful alternative to standard line charts

Chart created by the author

Hi, and welcome to this tutorial, where I’ll teach you to create the chart above using Python and Matplotlib.

What I like about this data visualization is its clean and beautiful way of showing how countries rank compared to each other on a particular metric.

The alternative to using a standard line chart showing the actual values get messy if some countries are close to each other or if some countries outperform others by a lot.

If you want access to the code for this tutorial, you can find it in this GitHub repository.

About the data

I’ve created a simple CSV containing GDP values for today’s ten largest economies for this tutorial.

Screenshot by the author

The data comes from the World Bank, and the full name of the indicator is “GDP (constant 2015 us$)”.

If you want to know more about different ways of measuring GDP, you can look at this story, where I use the same type of data visualization.

Let’s get on with the tutorial.

Step 1: Creating rankings

Step one is to rank the countries for each year in the dataset, which is easy to do with pandas.

def create_rank_columns(df, columns):
rank_columns = ["rank_{}".format(i) for i in range(len(columns))]
for i, column in enumerate(columns):
df[rank_columns[i]] = df[column].rank(ascending=False)

return df, rank_columns

The resulting columns look like this.

Source link

Leave a Comment