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:

figure; hold on; plot(x1, y1, 'Color', 'r'); plot(x2, y2, 'Color', 'b'); hold off; 

Using hold on keeps both lines on the same axes. Without it, the second plot() call would replace the first.

You can also pass color inline using the shorthand format string:

plot(x1, y1, 'r-'); % red solid line plot(x2, y2, 'b--'); % blue dashed line 

This shorthand combines color, line style, and marker in one compact string.

Method 2: Change Line Colors After Plotting 🎨

If you've already created a figure — or you're modifying an existing .fig file — you can change line colors by accessing the line handles returned by plot().

h1 = plot(x1, y1); hold on; h2 = plot(x2, y2); h1.Color = [0.1, 0.4, 0.8]; % custom blue h2.Color = [0.9, 0.2, 0.1]; % custom red 

Line handles are objects with properties you can get and set directly. Color, LineWidth, LineStyle, and DisplayName are all accessible this way.

If you didn't capture the handles at plot time, you can retrieve them afterward:

ax = gca; % get current axes lines = ax.Children; % returns all plotted objects lines(1).Color = 'green'; % modify first line lines(2).Color = 'black'; % modify second line 

Note that ax.Children returns objects in reverse order of how they were plotted — the most recently drawn line appears first. Keep this in mind when targeting specific lines.

Method 3: Modify the Axes Color Order Before Plotting

If you want MATLAB to automatically assign your chosen colors to lines in sequence, you can override the ColorOrder property of the axes before drawing anything:

figure; ax = axes; ax.ColorOrder = [1 0 0; 0 0 1]; % red, then blue hold(ax, 'on'); plot(ax, x1, y1); plot(ax, x2, y2); 

This approach is especially useful when generating plots programmatically or when you want consistent styling across multiple charts.

Working with Existing .fig Files

Opening a saved MATLAB figure and modifying line colors follows the same logic — open the file, access the axes children, and update their Color property:

openfig('myfile.fig'); ax = gca; lines = findobj(ax, 'Type', 'line'); lines(1).Color = '#3A86FF'; lines(2).Color = '#FF006E'; 

The findobj() function is particularly useful when a figure contains multiple object types (patches, scatter plots, etc.) and you only want to target lines specifically.

Variables That Affect Which Approach Works Best

FactorImpact
MATLAB versionHex color strings only work in R2019b+; color order behavior changed in R2014b
Number of linesMore lines benefit from ColorOrder or loop-based assignment
Plot typeplot(), line(), scatter(), and stairs() each have slightly different property structures
Working from a saved figureRequires handle retrieval rather than assignment at plot time
Script vs. interactive useInteractive users may prefer the Property Inspector GUI; scripts need programmatic methods

The GUI Alternative

For users who prefer not to write code for this task, MATLAB's Property Inspector (accessible by double-clicking a line in a figure window) lets you change line color, width, and style through a visual interface. This works well for one-off adjustments but doesn't scale to automated workflows.

Where Individual Setup Changes the Answer

The "right" method depends heavily on your workflow. Someone generating dozens of figures in a batch script has very different needs than someone tweaking a single figure for a paper. The MATLAB version installed on your machine determines which color format options are available. Whether you're starting from scratch or editing a saved figure changes which retrieval method is appropriate.

Understanding all three core methods — inline color arguments, post-plot handle editing, and ColorOrder presets — means you can choose the one that fits the context, rather than forcing one approach onto every situation.