0
0
MATLABdata~5 mins

Line styles, markers, and colors in MATLAB

Choose your learning style9 modes available
Introduction

We use line styles, markers, and colors to make plots easier to understand and more visually appealing.

When you want to show different data series clearly in one plot.
When you want to highlight specific data points on a graph.
When you want to make your graph colorful and easy to read.
When you want to distinguish lines by style instead of color (for printing).
When you want to customize the look of your plot to match a report or presentation style.
Syntax
MATLAB
plot(x, y, 'LineStyle', lineStyle, 'Marker', marker, 'Color', color)

LineStyle can be '-', '--', ':', '-.'

Marker can be 'o', '+', '*', '.', 'x', 's', 'd', '^', 'v', '>', '<', 'p', 'h'

Examples
This plots a red dashed line with circle markers.
MATLAB
plot(x, y, 'LineStyle', '--', 'Marker', 'o', 'Color', 'r')
This plots a dash-dot line with square markers in the default color.
MATLAB
plot(x, y, '-.s')
This plots a green line with star markers using RGB color values.
MATLAB
plot(x, y, 'Color', [0 0.5 0], 'Marker', '*')
Sample Program

This code plots two data series with different line styles, markers, and colors to show how to customize plots in MATLAB.

MATLAB
x = 1:5;
y1 = [2 4 6 8 10];
y2 = [1 3 5 7 9];

plot(x, y1, 'LineStyle', '--', 'Marker', 'o', 'Color', 'b')
hold on
plot(x, y2, '-.', 'Marker', 's', 'Color', [1 0 0])
hold off
xlabel('X axis')
ylabel('Y axis')
title('Line styles, markers, and colors example')
legend({'Series 1', 'Series 2'})
OutputSuccess
Important Notes

You can combine short style strings like '-.s' instead of using name-value pairs.

Use hold on to plot multiple lines on the same figure.

Colors can be specified by short names ('r', 'g', 'b') or RGB triplets like [1 0 0].

Summary

Line styles change how the line looks (solid, dashed, dotted).

Markers show points on the line (circles, squares, stars).

Colors make lines easy to tell apart and more attractive.