How to change two line colors in a MATLAB figure
You change line colors in MATLAB by setting the Color property when you create the plot or by using the set() function afterward. The simplest approach is to add a color argument directly to your plot() command. If you already have a figure with two lines and want to change their colors, you can reference each line object and modify its color property individually.
MATLAB accepts colors as named strings (like 'red' or 'blue'), RGB triplets (like [1 0 0] for red), or hexadecimal codes (like '#FF0000'). Each method works the same way — you just need to know which line you're targeting and what color you want it to be.
Key Takeaways
- Add a color directly in the plot() command by typing plot(x, y, 'color', 'red') or use shorthand like plot(x, y, 'r') for red.
- If your lines are already plotted, store them as objects when you create them: line1 = plot(x1, y1); then change color with set(line1, 'Color', 'blue').
- MATLAB accepts color names ('red', 'blue', 'green'), RGB values ([1 0 0]), and hex codes ('#FF0000') — all three methods work identically.
- Use gca (get current axes) to reference lines in an existing figure without storing them: set(gca.Children(1), 'Color', 'red').
Setting colors when you first create the plot
The easiest time to set line colors is when you create the plot. Use the Color name-value pair inside the plot() function. Here's the basic structure:
plot(x1, y1, 'Color', 'red') hold on plot(x2, y2, 'Color', 'blue')
The hold on command keeps the first line visible when you add the second one. Without it, the second plot replaces the first. Replace 'red' and 'blue' with whatever colors you need.
You can also use single-letter shortcuts: 'r' for red, 'b' for blue, 'g' for green, 'k' for black, 'm' for magenta, 'c' for cyan, 'y' for yellow, and 'w' for white. This shorthand is faster to type but only works with these eight colors.
Changing colors of lines that are already plotted
If your lines are already on the figure and you want to change their colors, you need to reference each line as an object. The cleanest way is to store each line when you create it:
line1 = plot(x1, y1); hold on line2 = plot(x2, y2); set(line1, 'Color', 'red') set(line2, 'Color', 'blue')
The set() function modifies properties of the line object. The first argument is the line you're changing, and the second and third arguments are the property name and its new value. This approach works even if you created the plot earlier in your script — as long as the line object is still in memory, you can modify it.
If you did not store the lines as objects when you created them, you can still access them through the current axes. Use gca (get current axes) to reference the figure:
set(gca.Children(1), 'Color', 'red') set(gca.Children(2), 'Color', 'blue')
Note that gca.Children(1) refers to the most recently plotted line, and gca.Children(2) refers to the one before it. The order is reversed from the order you plotted them.
Using RGB values and hexadecimal codes for custom colors
Named colors cover the basics, but MATLAB also accepts RGB triplets and hexadecimal codes if you need a specific shade. An RGB triplet is three numbers between 0 and 1, representing red, green, and blue intensity:
set(line1, 'Color', [1 0 0]) — pure red set(line1, 'Color', [0 1 0]) — pure green set(line1, 'Color', [0.5 0.5 0.5]) — medium gray set(line1, 'Color', [1 0.5 0]) — orange
Hexadecimal codes work the same way and are often easier if you're copying a color from a design tool or website:
set(line1, 'Color', '#FF0000') — red set(line1, 'Color', '#00FF00') — green set(line1, 'Color', '#FFA500') — orange
Both RGB and hex codes give you millions of color options instead of just the eight named colors. Use whichever format is easiest for your workflow.
Changing line width and style at the same time
While you're modifying line colors, you often want to adjust other properties like thickness or line style. The set() function handles multiple properties in one command:
set(line1, 'Color', 'red', 'LineWidth', 2, 'LineStyle', '--')
This sets line1 to red, makes it twice as thick as the default, and changes it to a dashed line. Common LineStyle values are '-' (solid), '--' (dashed), ':' (dotted), and '-.' (dash-dot). LineWidth is a number in points; the default is 0.5, and values like 1, 1.5, or 2 are typical.
You can also set these properties in the plot() command itself when you first create the line:
plot(x1, y1, 'Color', 'red', 'LineWidth', 2, 'LineStyle', '--')
This approach is cleaner if you know all your formatting before you plot, because everything happens in one place and the code is easier to read.
Troubleshooting common color-change problems
If you set a color and nothing changes, the most common cause is that you're referencing the wrong line object. Double-check that the variable you stored actually contains the line you think it does. You can verify this by typing the variable name in the command window — MATLAB will show you the line object's properties.
Another frequent issue is using the wrong syntax. Make sure you're using set() with the line object as the first argument, not the axes or figure. If you're using gca.Children, remember that the order is reversed: the last line you plotted is Children(1).
If you're using RGB values, remember that each number must be between 0 and 1, not 0 and 255. If you're used to web colors, this is the main difference — divide your web color values by 255 to convert them to MATLAB format. For example, RGB(255, 128, 0) in web format becomes [1, 0.502, 0] in MATLAB.
Frequently Asked Questions
Can I change line colors without storing them as objects?
Yes. Use gca.Children() to reference lines in the current figure. The most recent line is Children(1), the previous one is Children(2), and so on. This works if you did not store the lines when you created them, but you need to know the plotting order.
What's the difference between 'Color' and 'color' in the set() function?
MATLAB property names are case-insensitive, so 'Color', 'color', and 'COLOR' all work the same way. Use whichever you prefer — most people use 'Color' with a capital C by convention.
How do I change line colors in a loop?
Store each line in a cell array or vector as you create it, then loop through and set colors. For example: lines(i) = plot(x, y); inside the loop, then set(lines(1), 'Color', 'red') and set(lines(2), 'Color', 'blue') after the loop.
Do I need to use hold on if I'm storing lines as objects?
Yes. hold on prevents MATLAB from clearing the figure when you plot the second line. Without it, only the most recent plot appears, even if you stored the first line as an object.
What if I want to change the color of just one line in a figure with many lines?
If you stored all lines as objects, reference the specific one you want: set(line5, 'Color', 'green'). If you did not store them, use gca.Children() with the correct index number, counting backward from the most recent line.