Plot Multiple Lines in Python Using Matplotlib

One of the most common tasks I encounter is visualizing data effectively. When it comes to plotting multiple lines on a single graph, Matplotlib is my go-to library.

It’s useful, flexible, and widely used in the data science and analytics community, especially here in the USA, where data-driven decisions are crucial. Plotting multiple lines can help you compare trends, analyze relationships, or simply present your data clearly.

Let’s get in.

Get Started with Matplotlib

Before getting into plotting multiple lines, ensure you have Matplotlib installed. You can install it using pip if you haven’t already:

pip install matplotlib

Now, let’s explore different methods to plot multiple lines.

Method 1: Plot Multiple Lines Using Multiple plt.plot() Calls

This is the simplest way to plot multiple lines. You call plt.plot() in Python for each line you want to draw.

import matplotlib.pyplot as plt

# Sample quarterly sales data for three states
quarters = ['Q1', 'Q2', 'Q3', 'Q4']
california_sales = [25000, 27000, 30000, 32000]
texas_sales = [22000, 24000, 26000, 28000]
newyork_sales = [20000, 21000, 23000, 25000]

plt.plot(quarters, california_sales, label='California')
plt.plot(quarters, texas_sales, label='Texas')
plt.plot(quarters, newyork_sales, label='New York')

plt.title('Quarterly Sales Comparison (2025)')
plt.xlabel('Quarter')
plt.ylabel('Sales (USD)')
plt.legend()
plt.show()

I executed the above example code and added the screenshot below.

matplotlib plot multiple lines

This method is simple and works perfectly for a few lines. Each plt.plot() call adds a line to the same graph.

Read Matplotlib Secondary y-Axis

Method 2: Plot Multiple Lines in a Single plt.plot() Call

You can also plot multiple lines by passing multiple x and y pairs in a single plt.plot() call.

import matplotlib.pyplot as plt

quarters = ['Q1', 'Q2', 'Q3', 'Q4']
california_sales = [25000, 27000, 30000, 32000]
texas_sales = [22000, 24000, 26000, 28000]
newyork_sales = [20000, 21000, 23000, 25000]

plt.plot(quarters, california_sales, quarters, texas_sales, quarters, newyork_sales)
plt.legend(['California', 'Texas', 'New York'])

plt.title('Quarterly Sales Comparison (2025)')
plt.xlabel('Quarter')
plt.ylabel('Sales (USD)')
plt.show()

I executed the above example code and added the screenshot below.

matplotlib multiple lines

While this reduces the number of lines in your code, it’s less flexible if you want to customize individual lines (colors, markers, etc.).

Check out Matplotlib Set Axis Range

Method 3: Use a Loop to Plot Multiple Lines from Data Structures

For larger datasets or when working with data stored in dictionaries or DataFrames, looping through your data is efficient.

import matplotlib.pyplot as plt

quarters = ['Q1', 'Q2', 'Q3', 'Q4']
sales_data = {
    'California': [25000, 27000, 30000, 32000],
    'Texas': [22000, 24000, 26000, 28000],
    'New York': [20000, 21000, 23000, 25000]
}

for state, sales in sales_data.items():
    plt.plot(quarters, sales, label=state)

plt.title('Quarterly Sales Comparison (2025)')
plt.xlabel('Quarter')
plt.ylabel('Sales (USD)')
plt.legend()
plt.show()

I executed the above example code and added the screenshot below.

plot multiple lines matplotlib

This approach scales well and keeps your code clean, especially when the number of lines isn’t fixed.

Method 4: Customize Lines for Better Visualization

Matplotlib allows you to customize each line’s color, style, and markers, which can make your chart more readable.

import matplotlib.pyplot as plt

quarters = ['Q1', 'Q2', 'Q3', 'Q4']
sales_data = {
    'California': ([25000, 27000, 30000, 32000], 'r-o'),
    'Texas': ([22000, 24000, 26000, 28000], 'g--s'),
    'New York': ([20000, 21000, 23000, 25000], 'b-.^')
}

for state, (sales, style) in sales_data.items():
    plt.plot(quarters, sales, style, label=state)

plt.title('Quarterly Sales Comparison (2025)')
plt.xlabel('Quarter')
plt.ylabel('Sales (USD)')
plt.legend()
plt.show()

Here, 'r-o' means red line with circle markers, 'g--s' is green dashed line with square markers, and 'b-.^' is blue dash-dot line with triangle markers.

Read What is the add_axes Matplotlib

Method 5: Plot Multiple Lines with Pandas DataFrame

If you are working with tabular data, Pandas integrates well with Matplotlib.

import pandas as pd
import matplotlib.pyplot as plt

data = {
    'Quarter': ['Q1', 'Q2', 'Q3', 'Q4'],
    'California': [25000, 27000, 30000, 32000],
    'Texas': [22000, 24000, 26000, 28000],
    'New York': [20000, 21000, 23000, 25000]
}

df = pd.DataFrame(data)
df.set_index('Quarter', inplace=True)

df.plot(marker='o')
plt.title('Quarterly Sales Comparison (2025)')
plt.xlabel('Quarter')
plt.ylabel('Sales (USD)')
plt.show()

This method is very convenient for those who already use Pandas for data manipulation.

Tips for Effective Multi-Line Plots

  • Use legends: Always label your lines to avoid confusion.
  • Choose contrasting colors: This helps distinguish lines clearly.
  • Add markers: Markers help identify data points on lines.
  • Keep it simple: Don’t overcrowd your chart with too many lines.
  • Use gridlines: They improve readability.

Plotting multiple lines in Matplotlib is an essential skill for anyone working with data visualization in Python. The methods I shared here have helped me handle a variety of projects, from business analytics to scientific research.

Try these techniques with your datasets, and you’ll find it’s easier than ever to create clear, insightful graphs that tell a story.

This tutorial is based on my firsthand experience and best practices to help you get started quickly and confidently with multi-line plots in Matplotlib.

You may like to read:

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.