Plot Multiple Lines from Arrays in Matplotlib

I’ve been working with Python and data visualization for years, and one of the most common tasks I encounter is plotting multiple lines on the same graph. Matplotlib, Python’s go-to plotting library, makes this easy.

If you’re new to plotting multiple lines from arrays, it can feel a bit tricky at first. In this tutorial, I’ll walk you through several practical methods to plot multiple lines from NumPy arrays using Matplotlib. These methods are based on real-world examples, so you’ll find them easy to apply in your projects.

Let’s get in.

Methods to Plot Multiple Lines from Arrays

In the USA, data often comes in array form, sales numbers by month, temperature readings by city, or election poll results by state. Plotting multiple lines helps you compare these data sets visually and spot trends or anomalies quickly.

Using arrays (especially NumPy arrays) is efficient because they handle large datasets seamlessly and integrate well with Matplotlib.

Method 1: Plot Multiple Lines Using a Loop

The most flexible way I use to plot multiple lines is by looping through Python arrays. This allows me to easily customize each line’s color, style, and label.

Here’s a simple example where I plot monthly sales data for three US regions:

import numpy as np
import matplotlib.pyplot as plt

# Sample data: monthly sales (in thousands) for three regions
months = np.array(['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun'])
sales_northeast = np.array([25, 30, 28, 35, 40, 45])
sales_midwest = np.array([20, 22, 25, 27, 30, 33])
sales_southwest = np.array([15, 18, 20, 22, 25, 28])

# Combine sales data into one 2D array
all_sales = np.array([sales_northeast, sales_midwest, sales_southwest])

# Region labels
regions = ['Northeast', 'Midwest', 'Southwest']

# Plotting
plt.figure(figsize=(10, 6))

for i in range(all_sales.shape[0]):
    plt.plot(months, all_sales[i], marker='o', label=regions[i])

plt.title('Monthly Sales by Region (in thousands)')
plt.xlabel('Month')
plt.ylabel('Sales')
plt.legend()
plt.grid(True)
plt.show()

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

Multiple Lines from Arrays Matplotlib
  • You can handle any number of lines dynamically.
  • Easy to add legends and customize each line’s appearance.

Check out Matplotlib Legend Font Size

Method 2: Plot Multiple Lines Directly by Passing Arrays

If you have a smaller number of lines, you can pass each array directly to plt.plot() in one call.

Here’s how it looks with the same sales data:

import numpy as np
import matplotlib.pyplot as plt

months = np.array(['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun'])
sales_northeast = np.array([25, 30, 28, 35, 40, 45])
sales_midwest = np.array([20, 22, 25, 27, 30, 33])
sales_southwest = np.array([15, 18, 20, 22, 25, 28])

plt.figure(figsize=(10, 6))

plt.plot(months, sales_northeast, marker='o', label='Northeast')
plt.plot(months, sales_midwest, marker='s', label='Midwest')
plt.plot(months, sales_southwest, marker='^', label='Southwest')

plt.title('Monthly Sales by Region (in thousands)')
plt.xlabel('Month')
plt.ylabel('Sales')
plt.legend()
plt.grid(True)
plt.show()

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

Multiple Lines from Arrays in Matplotlib

This method is easy but less scalable when you have many lines.

Method 3: Use Matplotlib with NumPy Arrays and Automatic Colors

If you want to plot multiple lines and let Matplotlib handle colors automatically, you can use NumPy’s transpose to iterate over columns.

Imagine you have temperature data for three US cities over six months:

import numpy as np
import matplotlib.pyplot as plt

months = np.array(['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun'])
temperatures = np.array([
    [30, 32, 45, 55, 65, 70],  # New York
    [25, 28, 40, 50, 60, 68],  # Chicago
    [40, 45, 55, 65, 75, 80]   # Los Angeles
])

cities = ['New York', 'Chicago', 'Los Angeles']

plt.figure(figsize=(10, 6))

for temp, city in zip(temperatures, cities):
    plt.plot(months, temp, marker='o', label=city)

plt.title('Average Monthly Temperatures (°F)')
plt.xlabel('Month')
plt.ylabel('Temperature')
plt.legend()
plt.grid(True)
plt.show()

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

Plot Multiple Lines from Arrays in Matplotlib

This method is clean and works well when your data is structured with each row representing a line.

Read Matplotlib Multiple Plots

Method 4: Plot Multiple Lines with Different Colors and Legends

Sometimes, you want more control over colors and legends, especially for presentations or reports.

You can specify colors explicitly like this:

import numpy as np
import matplotlib.pyplot as plt

months = np.array(['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun'])
sales = np.array([
    [25, 30, 28, 35, 40, 45],  # Northeast
    [20, 22, 25, 27, 30, 33],  # Midwest
    [15, 18, 20, 22, 25, 28]   # Southwest
])

regions = ['Northeast', 'Midwest', 'Southwest']
colors = ['#1f77b4', '#ff7f0e', '#2ca02c']  # Blue, Orange, Green

plt.figure(figsize=(10, 6))

for i, region in enumerate(regions):
    plt.plot(months, sales[i], marker='o', color=colors[i], label=region)

plt.title('Monthly Sales by Region with Custom Colors')
plt.xlabel('Month')
plt.ylabel('Sales (thousands)')
plt.legend()
plt.grid(True)
plt.show()

This approach improves the clarity of your charts, especially when printing or sharing with stakeholders.

Plotting multiple lines from arrays in Matplotlib is a fundamental skill for any Python developer working with data visualization. Whether you prefer looping through data arrays, passing multiple arrays directly, or customizing colors and markers, Matplotlib offers flexible options that fit your needs.

From my experience, the loop method is the most scalable and adaptable for real-world US data sets, especially when working with many lines or dynamically changing data.

I hope this tutorial helps you create clear, insightful charts for your projects.

Happy plotting!

You may also 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.