Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to set same color for markers and lines in a Matplotlib plot loop?
When plotting multiple datasets in Matplotlib, you often want the markers and lines to share the same color for visual consistency. This can be achieved by letting Matplotlib automatically assign colors or by explicitly setting the color parameter.
Method 1: Using Automatic Color Cycling
Matplotlib automatically cycles through colors when you plot multiple lines. To ensure markers and lines match, plot them in the same call ?
import numpy as np
import itertools
import matplotlib.pyplot as plt
# Set up data
m = 5
n = 5
marker = itertools.cycle(('o', 'v', '^', '<', '>', 's', '8', 'p'))
plt.figure(figsize=(10, 6))
for i in range(1, n):
x = np.dot(i, [1, 1.1, 1.2, 1.3])
y = x ** 2
# Plot line and markers together with same color
plt.plot(x, y, linestyle='-', marker=next(marker),
markeredgecolor='none', alpha=1, label=f'Dataset {i}')
plt.legend()
plt.title('Same Color for Markers and Lines')
plt.xlabel('X values')
plt.ylabel('Y values')
plt.show()
Method 2: Explicitly Setting Colors
For more control, you can explicitly define colors and assign them to both markers and lines ?
import numpy as np
import itertools
import matplotlib.pyplot as plt
# Define colors and markers
colors = ['red', 'blue', 'green', 'orange', 'purple']
markers = ['o', 'v', '^', 's', 'p']
plt.figure(figsize=(10, 6))
for i in range(1, 5):
x = np.dot(i, [1, 1.1, 1.2, 1.3])
y = x ** 2
color = colors[i-1]
marker = markers[i-1]
# Plot with explicit color
plt.plot(x, y, linestyle='-', marker=marker, color=color,
markeredgecolor='none', label=f'Dataset {i}')
plt.legend()
plt.title('Explicitly Set Colors for Markers and Lines')
plt.xlabel('X values')
plt.ylabel('Y values')
plt.show()
Method 3: Using Color Cycle with Line Properties
You can also store the line object and reuse its color property ?
import numpy as np
import matplotlib.pyplot as plt
plt.figure(figsize=(10, 6))
markers = ['o', 'v', '^', 's', 'p']
for i in range(1, 5):
x = np.dot(i, [1, 1.1, 1.2, 1.3])
y = x ** 2
# Plot line first and get its color
line, = plt.plot(x, y, linestyle='-', label=f'Dataset {i}')
# Plot markers with same color as line
plt.plot(x, y, linestyle='', marker=markers[i-1],
color=line.get_color(), markeredgecolor='none')
plt.legend()
plt.title('Using Line Color Property')
plt.xlabel('X values')
plt.ylabel('Y values')
plt.show()
Comparison
| Method | Advantages | Best For |
|---|---|---|
| Single plot() call | Simple, automatic color cycling | Basic plots with default colors |
| Explicit colors | Full control over colors | When specific colors are required |
| Line color property | Flexible, works with any color scheme | Complex plots with dynamic styling |
Conclusion
The simplest approach is to plot lines and markers in a single plot() call, letting Matplotlib handle color consistency automatically. For more control, explicitly set colors or use the line object's color property.
