How to avoid line color repetition in matplotlib.pyplot?

When plotting multiple lines in matplotlib, the library automatically cycles through a default color sequence. To avoid line color repetition, you can manually specify unique colors using several approaches.

Method 1: Using Hexadecimal Color Codes

Specify unique hexadecimal color values for each line to ensure no repetition ?

import numpy as np
import matplotlib.pyplot as plt

# Set the figure size
plt.rcParams["figure.figsize"] = [10, 6]
plt.rcParams["figure.autolayout"] = True

# Create data points
x = np.linspace(0, 10, 100)

# Plot multiple lines with unique hex colors
plt.plot(x, np.sin(x), color="#FF5733", label="sin(x)", linewidth=2)
plt.plot(x, np.cos(x), color="#33A1FF", label="cos(x)", linewidth=2)
plt.plot(x, np.tan(x/2), color="#7D3C98", label="tan(x/2)", linewidth=2)
plt.plot(x, np.exp(-x/5), color="#2ECC71", label="exp(-x/5)", linewidth=2)

plt.legend()
plt.ylim(-2, 2)
plt.xlabel("X values")
plt.ylabel("Y values")
plt.title("Multiple Lines with Unique Colors")
plt.show()

Method 2: Using Named Colors

Matplotlib supports over 140 named colors that you can use directly ?

import numpy as np
import matplotlib.pyplot as plt

# Create data
x = np.linspace(0, 8, 100)

# Plot with named colors
plt.plot(x, x**0.5, color="crimson", label="sqrt(x)", linewidth=2)
plt.plot(x, x, color="forestgreen", label="x", linewidth=2)
plt.plot(x, x**1.5, color="royalblue", label="x^1.5", linewidth=2)
plt.plot(x, x**2, color="darkorange", label="x^2", linewidth=2)

plt.legend()
plt.xlabel("X values")
plt.ylabel("Y values")
plt.title("Named Colors Example")
plt.show()

Method 3: Using Color Palette

Generate a custom color palette to ensure unique colors for multiple lines ?

import numpy as np
import matplotlib.pyplot as plt

# Create data
x = np.linspace(0, 6, 100)
functions = [np.sin, np.cos, lambda x: np.sin(2*x), lambda x: np.cos(2*x), 
             lambda x: np.sin(x/2), lambda x: np.cos(x/2)]
labels = ["sin(x)", "cos(x)", "sin(2x)", "cos(2x)", "sin(x/2)", "cos(x/2)"]

# Custom color palette
colors = ['#FF6B6B', '#4ECDC4', '#45B7D1', '#96CEB4', '#FFEAA7', '#DDA0DD']

# Plot multiple functions with unique colors
plt.figure(figsize=(12, 8))
for i, (func, label, color) in enumerate(zip(functions, labels, colors)):
    plt.plot(x, func(x), color=color, label=label, linewidth=2)

plt.legend()
plt.xlabel("X values")
plt.ylabel("Y values")
plt.title("Multiple Functions with Color Palette")
plt.grid(True, alpha=0.3)
plt.show()

Comparison of Methods

Method Advantages Best For
Hexadecimal Colors Precise color control, unlimited options Custom branding, specific color requirements
Named Colors Easy to remember, readable code Quick plots, standard presentations
Color Palette Harmonious colors, scalable Multiple lines, professional visualizations

Conclusion

Use hexadecimal codes for precise color control, named colors for simplicity, or color palettes for multiple lines. This ensures each line has a unique, visually distinct color in your matplotlib plots.

Updated on: 2026-03-26T18:59:34+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements