Creating a Color-Coded Line Plot Using Pandas and Matplotlib in Python

Color Coding a Pandas Plot Based on Column Values

=====================================================

In this article, we’ll explore how to color code a pandas plot based on column values. We’ll discuss the basics of matplotlib, pandas, and color mapping, and provide examples of how to create a color-coded line plot.

Introduction


When working with data visualizations, it’s often useful to add color to the plot to represent different categories or values. In this article, we’ll show you how to achieve this using pandas and matplotlib in Python.

Background: Matplotlib and Color Mapping


Matplotlib is a popular plotting library for Python that provides a wide range of visualization tools. One of its most powerful features is color mapping, which allows us to map numerical values to specific colors.

Color mapping works by dividing the range of values into discrete segments, each associated with a particular color. The cm module in matplotlib provides a range of pre-defined color maps, including rainbow, plasma, inferno, and many more.

Code: Creating a Color-Coded Line Plot


Let’s start by creating a simple line plot using pandas and matplotlib:

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.cm as cm

# Create a sample DataFrame
sample = pd.DataFrame({'X': [1,2,3,1,2,3,1,2,1,2,3],
                   'Y': [1,1,1,2,2,2,3,3,4,4,4]})

# Set the figure and axis
plt.figure(figsize=(10,6))

# Plot the line plot
plt.plot(sample['X'], sample['Y'], linestyle='-', marker='o')

# Add title and labels
plt.title('Color-Coded Line Plot')
plt.xlabel('X')
plt.ylabel('Y')

# Show the plot
plt.show()

This code creates a simple line plot with a marker at each data point.

Coloring the Plot Based on Column Values


To color the plot based on column values, we can use the groupby function in pandas to group the data by the column value and then plot each group separately.

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.cm as cm

# Create a sample DataFrame
sample = pd.DataFrame({'X': [1,2,3,1,2,3,1,2,1,2,3],
                   'Y': [1,1,1,2,2,2,3,3,4,4,4]})

# Group the data by column values
colors = cm.rainbow(np.linspace(0, 1, len(sample['X'].unique())))
for i, group in enumerate(sample.groupby('X')):
    plt.plot(group['X'], group['Y'], c=colors[i])

# Add title and labels
plt.title('Color-Coded Line Plot')
plt.xlabel('X')
plt.ylabel('Y')

# Show the plot
plt.show()

This code groups the data by column values using groupby, and then plots each group separately with a different color.

Adding an Extra Column for Color Mapping


We can also add an extra column to our DataFrame to help us map the colors. Let’s create a sample DataFrame with an extra column called ‘G’:

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.cm as cm

# Create a sample DataFrame
sample = pd.DataFrame({'X': [1,2,3,1,2,3,1,2,1,2,3],
                   'Y': [1,1,1,2,2,2,3,3,4,4,4],
                   'G': [0,0,0,1,1,1,2,2,3,3,3]})

# Group the data by column values
colors = cm.rainbow(np.linspace(0, 1, len(sample['X'].unique())))
for name, group in sample.groupby('G'):
    plt.plot(group['X'], group['Y'], c=colors[name])

# Add title and labels
plt.title('Color-Coded Line Plot')
plt.xlabel('X')
plt.ylabel('Y')

# Show the plot
plt.show()

This code adds an extra column called ‘G’ to our DataFrame, which we can use to map the colors.

Conclusion


In this article, we’ve explored how to create a color-coded line plot using pandas and matplotlib in Python. We discussed the basics of color mapping and how to add an extra column to our DataFrame to help us map the colors.

By following these examples, you should now be able to create your own color-coded line plots with ease. Happy coding!


Last modified on 2025-04-21