How to Change the Color of Two Lines in a MATLAB Figure
Customizing line colors in MATLAB figures is one of the most common tasks in data visualization — whether you're comparing datasets, preparing publication-ready plots, or simply making a chart easier to read. MATLAB gives you several ways to control exactly which colors your lines use, and understanding how these methods work together gives you precise control over the final output.
How MATLAB Assigns Line Colors by Default
When you plot multiple lines on the same axes, MATLAB automatically cycles through a default color order — a predefined sequence of colors that gets applied in sequence. In newer versions of MATLAB (R2014b and later), this default palette includes blues, reds, yellows, and purples in a specific order.
The key thing to understand: MATLAB assigns these colors at the time the line is drawn, not retroactively. If you want to change colors, you either set them before plotting or after by accessing the line objects directly.
Method 1: Set Colors Directly in the Plot Command
The most straightforward approach is to specify the color as part of your plot() call. MATLAB accepts colors in several formats:
- Short color codes: 'r' (red), 'b' (blue), 'g' (green), 'k' (black), 'm' (magenta), 'c' (cyan), 'y' (yellow), 'w' (white)
- RGB triplets: [0.2, 0.6, 0.8] — values between 0 and 1
- Hexadecimal color strings (R2019b+): '#FF5733'
To plot two lines with specific colors in a single figure: