MATLAB Plot
The plot function creates a 2D graph.
Usage
The plot function takes a variable number of arguments. If only one is specified, it is assumed that the values of this array should be plotted against the indices (i.e., 1:end). If the singular argument is in fact a matrix, each row is plotted as a distinct line in the same manner.
If two arguments are specified, the second is plotted as a function of the first. That is to say, each marker collects its x-axis component from the first array and its y-axis component from the second array. This also means it is possible to plot a singular point as plot(x,y).
Use hold on to accumulate plots into the same graphic. This is then disabled by hold off.
To attach labels, try:
title("foo")
xlabel("bar")
ylabel("baz")To insert a legend, try:
plot(spectra) legend(starnames)
Lines are strictly ordered by insertion. Note that the legend must match this ordering.
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" |
|
