5 Steps to Beautiful Line Charts in Python

How to use the full capabilities of Matplotlib to tell a more compelling story

Guillaume Weingertner
Towards Data Science
GDP Evolution over time of the 5 richest countries — Image by Author

A few months back I wrote an article about bar charts and how you could make them clear, self-explanatory, and visually pleasing to the audience in order to tell a more compelling story (link below).

In this article I look into line charts instead, which have other specificities that are worth exploring.

Matplotlib makes it quick and easy to plot data with off-the-shelf functions but the fine tuning steps take more effort.

I spent quite some time researching best practices to build compelling charts with Matplotlib, so you don’t have to.

The idea is to go from this…

… to that:

All images, unless otherwise noted, are by the author.

To illustrate the methodology, I used a public dataset containing countries’ GDP information over the past 50 years:

Source: World Bank national accounts data, and OECD National Accounts data files.
License URL: https://datacatalog.worldbank.org/public-licenses#cc-by
License Type: CC BY-4.0

After importing the necessary packages to read the data and build our graphs, I simply filtered on the Top 20 countries of 2022:

import pandas as pd
import matplotlib.pyplot as plt
from datetime import timedelta

# Read the data
df = pd.read_csv('88a1e584-0a94-4e73-b650-749332831ef4_Data.csv', sep=',')
df.drop(['Series Name', 'Series Code', 'Country Code']…

Source link

This post originally appeared on TechToday.