How to Design a Custom Matplotlib Theme to Transform Your Line Charts from Basic to Brilliant

Aayush Sethi
3 min readSep 8, 2024

--

Article thumbnail (image by author)

Line charts are something that is simple to make yet not easily mastered by everyone. In this article, we will learn to create line charts that are easy to read and helps you to convey your message while impressing your audience with its simplicity.

Step 1: Installing Packages

We will be using Seabornto make the line charts and Matplotlib for customizing the charts. For downloading the data I will be using Yahoo's yfinance package.

import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
import yfinance as yf

Step 2: Downloading the Stock Data

For this article, we will be using Apple and Google closing prices from 01/01/2020 to 01/01/2022.

# Set the start and end date
start_date = '2020-01-01'
end_date = '2022-01-01'

# Get the data
google_data = yf.download('GOOGL', start_date, end_date)
apple_data = yf.download('AAPL', start_date, end_date)
Image 1 β€” Overview of the datasets used

Step 3: Making the Line Chart πŸ“ˆ

In my opinion, what makes a good line chart is the readability, good color schemes, and a clear message that conveys the story in a quick glance. As an economics student and a practising data scientist, I have seen a lot of line charts that are either too messy or too plain.

To make a graph that stands out I like to do the following things:

  1. Never use an x-axis marker (we all know the date is a date 😁)
  2. Removing the y-axis markers (unless it is important to point them out)
  3. Moving the tickers from left to right as our eyes naturally follows the line and makes it easier to read the values
  4. Using title, subtitle, and annotations to convey the message for easy interpretation.
##--- The Economist Color palette: https://design-system.economist.com/foundations/colour/palettes#accent-primary

##--- fig size
plt.figure(figsize=(15,8))
plt.style.use('fivethirtyeight')


##--- Plot the Goole and Apple Stocks in Seaborn
sns.lineplot(
data=google_data,
x="Date", y='Close', label = 'Google', color = '#E3120B')

sns.lineplot(
data=apple_data,
x="Date", y='Close', label = 'Apple', color = '#141F52')


##--- Title And Subtitle
plt.rcParams['font.family'] = 'DeJavu Serif'
plt.rcParams['font.serif'] = ['Times New Roman Bold']
plt.suptitle('Apple Dominating Google in the Stock Market',fontsize=24, y=1)
plt.title('Closing Prices, January 2020 to January 2022',fontsize=16)

##-- Removing the x-axis and y-axis labels
plt.ylabel('')
plt.xlabel('')

##-- Legend: Moving it to outside the plot
plt.legend(bbox_to_anchor=(1.04, 1), loc="upper left")

##-- Moving Tickers to the right
plt.tick_params(axis='y', which='major', labelleft= False, labelright= True)

##-- Annotation with an arrow
annotation_date = '2021-11'
annotation_value = 145
plt.annotate("Apple Leaps Ahead by $40", xy=(mdates.datestr2num(annotation_date), annotation_value),
xytext=(mdates.datestr2num(annotation_date), annotation_value - 20),
arrowprops=dict(arrowstyle='->', color='gray'))


##-- Changing x-axis labels
x1 = ['2020-01' , '2020-04', '2020-07', '2020-10', '2021-01', '2021-04', '2021-07', '2021-10', '2022-01' ]
x2 = [ "Jan'20" , "Apr'20", "July'20", "Oct'20", "Jan'21", "Apr,21", "July'21", "Oct'21", "Jan'22" ]

plt.xticks(x1, x2)

The Final Result

Connect with me on LinkedIn

--

--