When you make plots with Python’s matplotlib package, the package chooses some default colors for you. In the code below, we draw the lines for
.
import matplotlib.pyplot as plt
import numpy as np
x = np.arange(0.0, 2.0, 0.01)
for i in range(1, 12):
y = x**2 + i
plt.plot(x, y)
plt.show()
Notice that the first and the 11th line have the same color. That’s because matplotlib only has 10 default colors. We can use the code snippet below to get the hex codes for these 10 colors:
print(plt.rcParams['axes.prop_cycle'].by_key()['color']) # ['#1f77b4', '#ff7f0e', '#2ca02c', '#d62728', '#9467bd', '#8c564b', '#e377c2', '#7f7f7f', '#bcbd22', '#17becf']
The code snippet below is an example of how to pick out specific default colors. For example, if we wanted all the lines to be green (the 3rd color from the bottom):
default_color = plt.rcParams['axes.prop_cycle'].by_key()['color']
for i in range(1, 12):
y = x**2 + i
plt.plot(x, y, color = default_color[2])
plt.show()
Update (2023-05-27): As commenter jared f points out, there are several other ways to define colors and get the default matplotlib colors. This reference describes all the different ways to do so.
References:
- Stack Overflow. “Get default line colour cycle.“

