15.1 C
London
Friday, July 5, 2024
HomePythonMatplotlib in PythonHow to Plot Multiple Lines in Matplotlib

How to Plot Multiple Lines in Matplotlib

Related stories

Learn About Opening an Automobile Repair Shop in India

Starting a car repair shop is quite a good...

Unlocking the Power: Embracing the Benefits of Tax-Free Investing

  Unlocking the Power: Embracing the Benefits of Tax-Free Investing For...

Income Splitting in Canada for 2023

  Income Splitting in Canada for 2023 The federal government’s expanded...

Can I Deduct Home Office Expenses on my Tax Return 2023?

Can I Deduct Home Office Expenses on my Tax...

Canadian Tax – Personal Tax Deadline 2022

  Canadian Tax – Personal Tax Deadline 2022 Resources and Tools...

You can display  multiple lines in a single Matplotlib plot by using the following syntax:

import matplotlib.pyplot as plt

plt.plot(df['column1'])
plt.plot(df['column2'])
plt.plot(df['column3'])

...
plt.show()

This tutorial provides several examples of how to plot multiple lines in one chart using the following pandas DataFrame:

import numpy as np 
import pandas as pd

#make this example reproducible
np.random.seed(0)

#create dataset
period = np.arange(1, 101, 1)
leads = np.random.uniform(1, 50, 100)
prospects = np.random.uniform(40, 80, 100)
sales = 60 + 2*period + np.random.normal(loc=0, scale=.5*period, size=100)
df = pd.DataFrame({'period': period, 
                   'leads': leads,
                   'prospects': prospects,
                   'sales': sales})

#view first 10 rows
df.head(10)


        period	    leads	prospects	    sales
0	1	27.891862	67.112661	62.563318
1	2	36.044279	50.800319	62.920068
2	3	30.535405	69.407761	64.278797
3	4	27.699276	78.487542	67.124360
4	5	21.759085	49.950126	68.754919
5	6	32.648812	63.046293	77.788596
6	7	22.441773	63.681677	77.322973
7	8	44.696877	62.890076	76.350205
8	9	48.219475	48.923265	72.485540
9	10	19.788634	78.109960	84.221815

Plot Multiple Lines in Matplotlib

The following code shows how to plot three individual lines in a single plot in matplotlib:

import matplotlib.pyplot as plt 

#plot individual lines
plt.plot(df['leads'])
plt.plot(df['prospects'])
plt.plot(df['sales'])

#display plot
plt.show()

Multiple lines in Matplotlib chart

Customize Lines in Matplotlib

You can also customize the color, style, and width of each line:

#plot individual lines with custom colors, styles, and widths
plt.plot(df['leads'], color='green')
plt.plot(df['prospects'], color='steelblue', linewidth=4)
plt.plot(df['sales'], color='purple', linestyle='dashed')

#display plot
plt.show()

Customize multiple lines in Matplotlib

Add a Legend in Matplotlib

You can also add a legend so you can tell the lines apart:

#plot individual lines with custom colors, styles, and widths
plt.plot(df['leads'], label='Leads', color='green')
plt.plot(df['prospects'], label='Prospects', color='steelblue', linewidth=4)
plt.plot(df['sales'], label='Sales', color='purple', linestyle='dashed')

#add legend
plt.legend()

#display plot
plt.show()

Add legend for multiple lines in Matplotlib

Add Axis Labels and Titles in Matplotlib

Lastly, you can add axis labels and a title to make the plot complete:

#plot individual lines with custom colors, styles, and widths
plt.plot(df['leads'], label='Leads', color='green')
plt.plot(df['prospects'], label='Prospects', color='steelblue', linewidth=4)
plt.plot(df['sales'], label='Sales', color='purple', linestyle='dashed')

#add legend
plt.legend()

#add axis labels and a title
plt.ylabel('Sales', fontsize=14)
plt.xlabel('Period', fontsize=14)
plt.title('Company Metrics', fontsize=16)

#display plot
plt.show()

You can find more Matplotlib tutorials here.

Subscribe

- Never miss a story with notifications

- Gain full access to our premium content

- Browse free from up to 5 devices at once

Latest stories