HOWTO · MATLAB

MATLAB RGB Triplet

Learn how to set MATLAB plot colors with RGB triplets for lines, scatter points, and individual bars.

On this page

To set a custom plot color in MATLAB, use an RGB triplet: a three-element vector in red, green, blue order, with each component from 0 to 1. Set a line’s Color property to one triplet, or provide an N-by-3 matrix to color N scatter points or bars individually.

Understanding RGB Triplets in MATLAB

An RGB triplet stores the intensities of red, green, and blue in that order. For example, [1 0 0] is red, [0 1 0] is green, and [0 0 1] is blue. Mixing components creates other colors: [0.5 0.5 0] combines red and green into a muted yellow. A component of 0 contributes none of that color; 1 contributes its full intensity.

Use one triplet when an entire graphics object should share a color. For per-point or per-bar colors, arrange one triplet per row in an N-by-3 matrix, where N is the number of plotted items.

Set a Line Color with an RGB Triplet

A line object’s Color property accepts one RGB triplet. This complete example plots a sine curve and sets its line to a custom reddish color.

warning('off', 'Octave:gnuplot-graphics');
set(0, 'defaultfigurevisible', 'off');
x = 0:0.1:10;
y = sin(x);
plot(x, y, 'Color', [0.8 0.2 0.2], 'LineWidth', 2);
title('Sine Wave with Custom Color');
xlabel('X-axis');
ylabel('Y-axis');
grid on;

Change the three values to adjust the red, green, and blue intensity. Keep each within the 0-to-1 range; the same triplet applies to the whole line.

Assign RGB Colors to Scatter-Plot Points

For individual scatter colors, pass an N-by-3 color matrix to scatter. Each row corresponds to the point at the same position in the x and y vectors. The following complete example uses four points and gives each a distinct color.

warning('off', 'Octave:gnuplot-graphics');
set(0, 'defaultfigurevisible', 'off');
x = [1 2 3 4];
y = [2 4 3 5];
colors = [1 0 0; 0 1 0; 0 0 1; 0.8 0.4 0];
scatter(x, y, 80, colors, 'filled');
xlabel('X');
ylabel('Y');
title('Scatter Points with RGB Colors');
grid on;

The matrix must have one row for every point, and its three columns hold red, green, and blue values. If you pass a single triplet instead, the points share that color.

Set Individual Bar Colors with RGB Triplets

In MATLAB, set the bar object’s FaceColor property to 'flat' and assign an N-by-3 matrix to its CData; each row supplies the color of one bar. For example, after creating bars = bar(values), use bars.FaceColor = 'flat'; bars.CData = colors;. This Bar object interface is MATLAB-specific. The runnable Octave example below draws each bar as a patch so each rectangle can receive its corresponding RGB triplet.

warning('off', 'Octave:gnuplot-graphics');
set(0, 'defaultfigurevisible', 'off');
values = [3 5 2 8];
colors = [0.2 0.6 0.8; 0.8 0.2 0.2; 0.2 0.8 0.2; 0.5 0.5 0.2];
hold on;
for k = 1:numel(values)
  x = k;
  patch([x-0.4 x+0.4 x+0.4 x-0.4], [0 0 values(k) values(k)], colors(k,:));
end
set(gca, 'XTick', 1:numel(values), 'XTickLabel', {'A', 'B', 'C', 'D'});
xlim([0.5 numel(values)+0.5]);
xlabel('Category');
ylabel('Value');
title('Bars with Individual RGB Colors');
grid on;

For one MATLAB bar series, the rows of CData map to the bars in order. If all bars should match, set FaceColor directly to a single RGB triplet instead of using flat face coloring and a color matrix. Octave’s bar handle does not expose MATLAB’s Bar.CData property, so use patch objects there when assigning distinct face colors.