A few months ago I wrote an article about bar charts and how to create them. clear, Self explanatory, and visually pleasing to the audience to tell a more compelling story (link below).
In this article I analyze line graphs On the other hand, they have other specificities that are worth exploring.
Matplotlib makes it quick and easy to plot data with out-of-the-box functions, but the tuning steps require more effort.
I spent quite a bit of time researching best practices for creating beautiful graphs with Matplotlib, so you don’t have to.
The idea is to start from this…
…to that one:
All images, unless otherwise noted, are the author’s.
To illustrate the methodology, I used a public data set containing GDP information for countries over the last 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 charts, I simply filtered out 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')…