Plot Multiple Lines with Different Colors in Matplotlib

I’ve worked extensively with data visualization. One of the most common tasks I encounter is plotting multiple lines on the same graph, each with a distinct color.

In this tutorial, I’ll walk you through several easy methods to plot multiple lines with different colors using Matplotlib. These methods are practical and easy to implement, helping you create clear and visually appealing charts for your data projects.

Let’s get started!

Methods to Plot Multiple Lines with Different Colors in Matplotlib

Now, I will explain to you the methods to Plot Multiple Lines with Different Colors in Matplotlib.

Read Matplotlib 2d Surface Plot

1: Use Matplotlib’s Default Color Cycle

Matplotlib automatically cycles through a set of default colors when you plot multiple lines. This is the quickest way to plot multiple lines with different colors without manually specifying colors.

import matplotlib.pyplot as plt

months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun']
california_sales = [25000, 27000, 30000, 32000, 35000, 37000]
texas_sales = [22000, 23000, 25000, 28000, 30000, 32000]
newyork_sales = [20000, 21000, 23000, 25000, 27000, 29000]

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

plt.title('Monthly Sales by State')
plt.xlabel('Month')
plt.ylabel('Sales ($)')
plt.legend()
plt.show()

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

Multiple Lines with Different Colors in Matplotlib

In this example, Matplotlib automatically assigns different colors to each line, making the chart easy to read.

Check out Matplotlib Unknown Projection ‘3d’

2: Manually Specifying Colors with a List

Sometimes you want full control over the colors used in your plot. You can define a list of colors and assign them explicitly to each line.

colors = ['#FF0000', '#800080', '#FFC0CB']  # Red, Purple, Pink

plt.plot(months, california_sales, color=colors[0], label='California')
plt.plot(months, texas_sales, color=colors[1], label='Texas')
plt.plot(months, newyork_sales, color=colors[2], label='New York')

plt.title('Monthly Sales by State')
plt.xlabel('Month')
plt.ylabel('Sales ($)')
plt.legend()
plt.show()

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

Plot Multiple Lines with Different Colors in Matplotlib

This approach is great when you want to match your company’s branding colors or maintain consistency across multiple charts.

Read What is the add_axes Matplotlib

3: Use a Colormap for Dynamic Coloring

If you have many lines to plot, manually choosing colors can be tedious. Instead, you can use a colormap to generate colors dynamically.

import numpy as np
import matplotlib.cm as cm

states = ['California', 'Texas', 'New York', 'Florida', 'Illinois']
sales_data = [
    [25000, 27000, 30000, 32000, 35000, 37000],
    [22000, 23000, 25000, 28000, 30000, 32000],
    [20000, 21000, 23000, 25000, 27000, 29000],
    [18000, 19000, 21000, 23000, 25000, 27000],
    [16000, 17000, 19000, 21000, 23000, 25000]
]

colors = cm.viridis(np.linspace(0, 1, len(states)))

for i, state in enumerate(states):
    plt.plot(months, sales_data[i], color=colors[i], label=state)

plt.title('Monthly Sales by State')
plt.xlabel('Month')
plt.ylabel('Sales ($)')
plt.legend()
plt.show()

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

Different Colors in Matplotlib Plot Multiple Lines

Using a colormap like viridis provides a visually appealing gradient of colors, which is especially useful for large datasets.

Check out Matplotlib Set Axis Range

4: Plot Multiple Lines in a Loop with Automatic Color Cycling

When working with many datasets, looping through your data and plotting each line is efficient. Matplotlib’s color cycle automatically assigns colors in this case.

for i, state in enumerate(states):
    plt.plot(months, sales_data[i], label=state)

plt.title('Monthly Sales by State')
plt.xlabel('Month')
plt.ylabel('Sales ($)')
plt.legend()
plt.show()

This method is quick and clean, especially when combined with Matplotlib’s default color cycle.

Read Matplotlib Secondary y-Axis

Bonus Tips for Better Line Plots

  • Add line markers: Use markers like circles or squares to highlight data points.
  • Adjust line styles: Differentiate lines further by using dashed or dotted styles.
  • Use legends wisely: Place legends outside the plot if you have many lines.
  • Label axes clearly: Always add axis labels and titles for context.

Mastering these methods will help you create clear, professional, and visually appealing line plots in Matplotlib. Whether you’re analyzing sales trends, stock prices, or any time series data relevant to the USA market, these techniques are essential to communicate your insights effectively.

Other Python tutorials you may like:

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.