Change line colors in MATLAB by specifying the color property when you plot, or by editing the line object after plotting
The simplest way to set line colors in MATLAB is to add a color argument directly to your plot() command. MATLAB accepts color names like 'red', 'blue', 'green', or single-letter shortcuts like 'r', 'b', 'g'. You can also use RGB values as a vector, like [0.5 0.2 0.8] for a purple shade. If you have already plotted the lines and want to change them, you can modify the line object's Color property after the fact.
The method you choose depends on whether you are setting colors while writing new code or editing an existing figure. Both approaches take seconds once you know the syntax.
Key Takeaways
- Add a color argument directly in the plot() command: plot(x, y, 'r') for red or plot(x, y, 'Color', [0.5 0.2 0.8]) for RGB values.
- Use single-letter color codes ('r', 'b', 'g', 'k', 'm', 'c', 'y', 'w') for quick, standard colors without typing full names.
- Retrieve and modify existing line objects using set() or dot notation: set(line1, 'Color', 'blue') or line1.Color = 'blue'.
- RGB vectors let you specify any color as three numbers between 0 and 1, where [1 0 0] is red, [0 1 0] is green, and [0 0 1] is blue.
- Store the output of plot() in a variable so you can edit line properties later without replotting.
Setting colors while plotting with the plot() command
The most direct approach is to specify the color when you call plot(). If you have two lines to plot, add a color argument to each plot statement. The syntax is straightforward: plot(x_data, y_data, 'color_code'). For example:
plot(x1, y1, 'r') hold on plot(x2, y2, 'b')
This plots the first line in red and the second in blue. The hold on command keeps both lines visible on the same axes. Single-letter codes are fast: 'r' for red, 'b' for blue, 'g' for green, 'k' for black, 'm' for magenta, 'c' for cyan, 'y' for yellow, 'w' for white.
If you want more control over the exact shade, use the 'Color' name-value pair with an RGB vector. RGB values range from 0 to 1 for each channel:
plot(x1, y1, 'Color', [1 0 0]) hold on plot(x2, y2, 'Color', [0 0 1])
This also produces red and blue, but you can adjust each number to create custom shades. For example, [0.5 0 0] is a darker red, and [0 0.5 1] is a light blue.
Changing colors of existing lines using line objects
If your lines are already plotted and you want to change their colors without rerunning the entire script, store the line objects when you create them. Assign the output of plot() to a variable:
line1 = plot(x1, y1) hold on line2 = plot(x2, y2)
Now line1 and line2 are objects that represent those lines. You can modify their Color property using either set() or dot notation. With set():
set(line1, 'Color', 'red') set(line2, 'Color', 'blue')
With dot notation, which is more modern and readable:
line1.Color = 'red' line2.Color = 'blue'
Both methods work identically. You can use color names, single-letter codes, or RGB vectors with either syntax. This approach is useful when you want to experiment with colors or when you are editing a figure interactively in the MATLAB command window.
Using RGB vectors for custom colors
RGB vectors give you access to millions of colors beyond the eight standard single-letter codes. Each value in the vector represents the intensity of red, green, and blue light, from 0 (off) to 1 (full intensity). Combining these three channels produces any color you need.
Common RGB values include [1 0 0] for pure red, [0 1 0] for pure green, [0 0 1] for pure blue, [0.5 0.5 0.5] for gray, and [1 1 0] for yellow. To create a custom shade, adjust each number. For example, [0.8 0.2 0.2] is a muted red-brown, and [0.2 0.8 0.8] is a cyan-green.
You can explore RGB colors in the same ways as named colors:
plot(x1, y1, 'Color', [0.8 0.2 0.2]) hold on plot(x2, y2, 'Color', [0.2 0.8 0.8])
Or modify existing lines:
line1.Color = [0.8 0.2 0.2] line2.Color = [0.2 0.8 0.8]
Combining color changes with other line properties
While changing colors, you often want to adjust other line properties at the same time. MATLAB lets you set multiple properties in one command. Common properties include LineWidth (thickness), LineStyle (solid, dashed, dotted), and Marker (point symbols).
Using set() with multiple properties:
set(line1, 'Color', 'red', 'LineWidth', 2, 'LineStyle', '--') set(line2, 'Color', 'blue', 'LineWidth', 1.5, 'LineStyle', ':')
Using dot notation for each property:
line1.Color = 'red' line1.LineWidth = 2 line1.LineStyle = '--'
LineStyle options include '-' for solid, '--' for dashed, ':' for dotted, and '-.' for dash-dot. LineWidth is measured in points, with typical values between 0.5 and 3. These adjustments help distinguish lines visually when colors alone are not enough.
Troubleshooting common color issues
If a color change does not appear, check that you are modifying the correct line object. When you use hold on, each plot() call creates a new line. Make sure you stored the output in a variable and are editing that specific variable, not an old one.
If you see an error like "Unrecognized property name 'Color'", you may be trying to set a property on something other than a line object. Verify that your variable actually contains the line by typing its name in the command window and checking the output type.
Color names are case-insensitive, so 'Red', 'RED', and 'red' all work. RGB vectors must be three numbers between 0 and 1. If you use values outside this range, MATLAB will either clip them or throw an error depending on your version.
Frequently Asked Questions
Can I change line colors after I close and reopen the figure?
No. Once you close a figure, the line objects are deleted. To change colors in a saved figure file, you must open it in MATLAB, modify the lines, and save it again. If you want to preserve your color choices, save the figure as a .fig file, which stores the line objects.
What is the difference between 'r' and 'Color', 'r' in the plot command?
'r' is a shorthand that sets color and line style together. 'Color', 'r' is the full name-value pair syntax. Both produce the same result, but the full syntax lets you combine color with other properties more clearly, like plot(x, y, 'Color', 'r', 'LineWidth', 2).
How do I match a specific color I see in another program?
Use a color picker tool to get the RGB values in the 0–255 range, then divide each by 255 to convert to MATLAB's 0–1 scale. For example, if a color picker shows RGB(200, 100, 50), use [200/255, 100/255, 50/255] or approximately [0.78, 0.39, 0.20] in MATLAB.
Can I use hexadecimal color codes like #FF0000?
Not directly in older MATLAB versions, but newer versions support hex codes as strings. Try plot(x, y, 'Color', '#FF0000') for red. If it does not work, convert the hex code to RGB by dividing each pair of digits by 255. #FF0000 becomes [1, 0, 0].
What happens if I plot a third line without specifying a color?
MATLAB cycles through a default color palette automatically. The first line is blue, the second is orange, the third is yellow, and so on. This default cycle repeats if you plot more than seven lines without specifying colors.