MATLAB Plot
The plot function creates a 2D graphic of a curve.
Usage
x = 0:pi/100:2*pi; y = sin(x); figure; plot(x,y);
See also figures and related commands.
Line Specifications
Basic line specifications can be made with a single string argument. More detailed specifications require named properties to be set individually.
Shorthand
Lines and markers are styled by passing a symbolic string as an argument. This string is composed of component specifications.
Line components are styled with the following specifications:
Name |
Specification |
Solid |
- |
Dashed |
-- |
Dotted |
: |
Dash-dotted |
-. |
Marker components are styled with the following specifications:
Name |
Specification |
Circle |
o |
Plus sign |
+ |
Asterisk |
* |
Point |
. |
Cross |
x |
Horizontal line |
_ |
Vertical line |
| |
Square |
s |
Diamond |
d |
Upward-pointing triangle |
^ |
Downward-pointing triangle |
v |
Right-pointing triangle |
> |
Left-pointing triangle |
< |
Pentagram |
pentagram |
Hexagram |
hexagram |
Note that "-." means a dash-dotted line, whereas ".-" means point markers and a line. This also demonstrates that line specifications do not need to follow an order.
Lastly, there are color components. As an example, red is specified by r. This is not generally useful though, as any plot with multiple lines should use different colors for each. Colors are automatically assigned in a reasonable manner.
Name |
Specification |
Equivalent RGB Triplet |
Equivalent hexadecimal code |
Red |
r |
[1 0 0] |
"#FF0000" |
Green |
g |
[0 1 0] |
"#00FF00" |
Blue |
b |
[0 0 1] |
"#0000FF" |
Cyan |
c |
[0 1 1] |
"#00FFFF" |
Magenta |
m |
[1 0 1] |
"#FF00FF" |
Yellow |
y |
[1 1 0] |
"#FFFF00" |
Black |
k |
[0 0 0] |
"#000000" |
White |
w |
[1 1 1] |
"#FFFFFF" |
None |
none |
|
|
The equivalences shown above are not useful for this shorthand, but are good to note for the below.
Properties
Try either of the following:
plot(x,y,LineWidth=3) plot(x,y,"LineWidth",3)
Note that the latter is considered the legacy, backwards-compatible method.
Name |
Meaning |
Domain |
Example |
Default |
Color |
Line color |
|
"r", [1 0 0] |
|
LineStyle |
Line style |
|
"--" |
"-" |
LineWidth |
Line width |
Positive numbers (pts) |
|
0.5 |
Marker |
Marker style |
|
"o" |
"none" |
MarkerIndices |
Markers to display |
Positive numbers |
[1 5 10] |
1:end |
MarkerEdgeColor |
Marker outline color |
|
"r", [1 0 0] |
"auto" |
MarkerFaceColor |
Marker fill color |
|
"r", [1 0 0] |
"none" |
MarkerSize |
Marker size |
positive numbers (pts) |
|
6 |
DatetimeTickFormat |
Format string for datetimes |
|
"yyyy-MM-dd" |
|
DurationTickFormat |
Format string for durations |
|
"dd:hh:mm:ss" |
|
