Matplotlib automatically assigns a sequence of colors to lines in a single Axes, and you can fully customize those colors—either by setting a color directly for each line or by changing the default color cycle that Matplotlib uses. This article explains how the automatic color cycle works, how to override it with custom colors, and how to control the cycle for consistent, readable plots.
Formula
The color cycle is a list of colors. When you plot a line without specifying a color, Matplotlib takes the next color from the cycle (cycling back to the start when it runs out). The formula for the color of the n-th line is:
color = cycle[ (start_index + n – 1) mod cycle_length ]
Where cycle is the list of colors (e.g., the default Tab10 has 10 colors), start_index is the position to start from (usually 0), n is the line number (1-based), and mod is the modulo operation. The calculator above shows the hex code for a given line number and cycle.
Worked Example
Suppose you plot 5 lines with the default color cycle (Tab10). The line numbers and colors are:
- Line 1: #1f77b4 (blue)
- Line 2: #ff7f0e (orange)
- Line 3: #2ca02c (green)
- Line 4: #d62728 (red)
- Line 5: #9467bd (purple)
Using the calculator with n=5, c=0, and Default cycle, the output is #9467bd, matching the fifth color. If you set the start index to 2, the sequence becomes: #2ca02c, #d62728, #9467bd, #8c564b, #e377c2—the cycle shifts by two positions.
How Matplotlib’s Automatic Color Cycle Works
Matplotlib uses a global color cycle (also called the “prop cycle”) that applies to lines, markers, and other artists. By default, the cycle is the Tab10 colormap, which contains 10 distinct colors. When you call plt.plot(x, y) repeatedly, each new line gets the next color in the cycle. After the 10th line, the 11th line repeats the first color.
You can see the current cycle with plt.rcParams['axes.prop_cycle']. The default is a cycler object that yields color, and sometimes other properties like linestyle. The cycle is shared across all Axes in a figure, so if you plot in two subplots, the second subplot continues where the first left off.
How to Set Custom Colors for Lines
There are several ways to specify a custom color for a line:
- Named colors:
plt.plot(x, y, color='red')or'c'for cyan. - Hex strings:
color='#ff5733'. - RGB/RGBA tuples:
color=(0.1, 0.2, 0.5)(values 0-1). - HTML color names:
color='firebrick'. - Grayscale:
color='0.5'(string representing gray level). - Short color codes:
'b'(blue),'g'(green),'r'(red),'c'(cyan),'m'(magenta),'y'(yellow),'k'(black),'w'(white).
You can also use a colormap to assign colors to multiple lines programmatically, e.g., plt.cm.viridis(i/n).
Changing the Default Color Cycle
To change the global color cycle for all plots in a script or notebook, use plt.rcParams['axes.prop_cycle']:
import matplotlib.pyplot as plt
from cycler import cycler
plt.rcParams['axes.prop_cycle'] = cycler(color=['#1f77b4', '#ff7f0e', '#2ca02c'])
You can also set it temporarily with a context manager:
with plt.rc_context({'axes.prop_cycle': cycler(color=['red', 'green', 'blue'])}):
plt.plot(x1, y1)
plt.plot(x2, y2)
For a single Axes, you can set the cycle after creation:
ax.set_prop_cycle(color=['red', 'green', 'blue'])
This overrides the global setting for that Axes only.
Choosing a Color Cycle for Accessibility
When selecting a color cycle, consider colorblind viewers. The default Tab10 is designed to be distinguishable for most color vision deficiencies, but it’s not perfect. The Colorblind-friendly cycles like Okabe-Ito (often used in scientific plots) or viridis (a perceptually uniform colormap) are safer choices. You can define your own cycle with colors that have high contrast and are distinguishable in grayscale.
For example, a common colorblind-safe palette is:
- #0072B2 (blue)
- #D55E00 (vermillion)
- #009E73 (bluish green)
- #CC79A7 (reddish purple)
- #F0E442 (yellow)
Use cycler(color=['#0072B2', '#D55E00', '#009E73', '#CC79A7', '#F0E442']).
Common Problems and Misconceptions
1. Lines repeat colors after 10 plots. That’s expected with the default cycle. To avoid repetition, either set a longer cycle or use a colormap with many colors.
2. Setting color in a loop doesn’t work. If you pass a list of colors to a single plot call, it might not apply as expected. Use a loop with individual plot calls or use the color parameter with a list for some plot types (like scatter).
3. The color cycle resets between figures. The cycle is global, so it continues across figures unless you reset it. Use plt.gca().set_prop_cycle(None) to reset to default for a specific Axes.
4. Misconception: ‘c’ is a valid color for cyan. Yes, it is, but it can be confused with the c parameter for color in some functions. Always use color='c' for clarity.
Frequently Asked Questions
How do I get a list of the current color cycle colors?
You can retrieve the current cycle with plt.rcParams['axes.prop_cycle'] and then extract colors: [c['color'] for c in plt.rcParams['axes.prop_cycle']]. This returns a list of hex strings.
Can I use a colormap as a color cycle?
Yes. You can create a cycle from a colormap using cycler(color=plt.cm.viridis(np.linspace(0,1,10))). This gives 10 evenly spaced colors from the viridis colormap.
Why do my line colors change when I use subplots?
Because the color cycle is global, subplots share the same cycle. If you plot in the first subplot, the second subplot continues from where the first left off. To reset per subplot, use ax.set_prop_cycle(None) or set a new cycle.
What is the difference between ‘color’ and ‘c’ in plot?
There is no difference; ‘c’ is an alias for ‘color’ in the plot function. However, in other functions like scatter, ‘c’ can be a sequence of values for colormapping, so it’s safer to use ‘color’ for a single color.
How do I make a custom color cycle permanent for all my plots?
Set it in a Matplotlib style file or in your script’s startup. For example, create a file mplstyle.mplstyle with axes.prop_cycle : cycler(color='1f77b4,ff7f0e,2ca02c') and load it with plt.style.use('mplstyle.mplstyle').
Can I use a list of colors for multiple lines in one plot call?
Yes, if you pass a 2D array to plot, you can specify a list of colors using the color parameter: plt.plot(x, y2d, color=['red', 'green', 'blue']). This works for 2D arrays where each column is a line.
Related
Formula used
in n | Number of lines | - | 5 | 1 | 20 in c | Cycle start index | - | 0 | 0 | 19 sel p | Color cycle | Default=0, Tab10=0, Tab20=1, Dark2=2, Set1=3, Paired=4 out col | Line color (hex) | - | (p==0 ? (c%10==0?'#1f77b4':c%10==1?'#ff7f0e':c%10==2?'#2ca02c':c%10==3?'#d62728':c%10==4?'#9467bd':c%10==5?'#8c564b':c%10==6?'#e377c2':c%10==7?'#7f7f7f':c%10==8?'#bcbd22':'#17becf') : p==1 ? (c%20==0?'#1f77b4':c%20==1?'#aec7e8':c%20==2?'#ff7f0e':c%20==3?'#ffbb78':c%20==4?'#2ca02c':c%20==5?'#98df8a':c%20==6?'#d62728':c%20==7?'#ff9896':c%20==8?'#9467bd':c%20==9?'#c5b0d5':c%20==10?'#8c564b':c%20==11?'#c49c94':c%20==12?'#e377c2':c%20==13?'#f7b6d2':c%20==14?'#7f7f7f':c%20==15?'#c7c7c7':c%20==16?'#bcbd22':c%20==17?'#dbdb8d':c%20==18?'#17becf':'#9edae5') : p==2 ? (c%8==0?'#1b9e77':c%8==1?'#d95f02':c%8==2?'#7570b3':c%8==3?'#e7298a':c%8==4?'#66a61e':c%8==5?'#e6ab02':c%8==6?'#a6761d':'#666666') : p==3 ? (c%9==0?'#e41a1c':c%9==1?'#377eb8':c%9==2?'#4daf4a':c%9==3?'#984ea3':c%9==4?'#ff7f00':c%9==5?'#ffff33':c%9==6?'#a65628':c%9==7?'#f781bf':'#999999') : (c%12==0?'#a6cee3':c%12==1?'#1f78b4':c%12==2?'#b2df8a':c%12==3?'#33a02c':c%12==4?'#fb9a99':c%12==5?'#e31a1c':c%12==6?'#fdbf6f':c%12==7?'#ff7f00':c%12==8?'#cab2d6':c%12==9?'#6a3d9a':c%12==10?'#ffff99':'#b15928')) | 0
Each line is one input (in, sel, list) or one result (out: label, unit, formula, decimals). How the calculators work
Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.
More cards in this drawer
- Delete Picture Kik? How to Remove Photos
- What Happens to X Replies, DMs, and Followers After Deletion?
- Data Breaches That Shook the World
- Disable Google Assistant: Turn It Off on Android & Home
- Resin vs FDM Printers for Miniatures: Detail, Durability, Cost
- Are Networked Implants Safe? What If One Fails?