Matlab Plotting Additional Notes
Matlab Plotting Additional Notes
Create x as a vector of linearly spaced values between 0 and 2π. Use an increment
of π/100 between the values. Create y as sine values of x. Create a line plot of the data.
x = 0:pi/100:2*pi;
y = sin(x);
plot(x,y)
Plot Multiple Lines
Try This Example
Define x as 100 linearly spaced values between −2π and 2π. Define y1 and y2 as sine and
cosine values of x. Create a line plot of both sets of data.
x = linspace(-2*pi,2*pi);
y1 = sin(x);
y2 = cos(x);
figure
plot(x,y1,x,y2)
16 2 3 13
5 11 10 8
9 7 6 12
4 14 15 1
Create a 2-D line plot of Y. MATLAB® plots each matrix column as a separate line.
figure
plot(Y)
Plot three sine curves with a small phase shift between each line. Use the default line style for
the first line. Specify a dashed line style for the second line and a dotted line style for the third
line.
x = 0:pi/100:2*pi;
y1 = sin(x);
y2 = sin(x-0.25);
y3 = sin(x-0.5);
figure
plot(x,y1,x,y2,'--',x,y3,':')
MATLAB® cycles the line color through the default color order.
Specify Line Style, Color, and Marker
Plot three sine curves with a small phase shift between each line. Use a green line with no
markers for the first sine curve. Use a blue dashed line with circle markers for the second sine
curve. Use only cyan star markers for the third sine curve.
x = 0:pi/10:2*pi;
y1 = sin(x);
y2 = sin(x-0.25);
y3 = sin(x-0.5);
figure
plot(x,y1,'g',x,y2,'b--o',x,y3,'c*')
Display Markers at Specific Data Points
Try This Example
Create a line plot and display markers at every fifth data point by specifying a marker symbol
and setting the MarkerIndices property as a name-value pair.
x = linspace(0,10);
y = sin(x);
plot(x,y,'-o','MarkerIndices',1:5:length(y))
Specify Line Width, Marker Size, and Marker Color
Create a line plot and use the LineSpec option to specify a dashed green line with square
markers. Use Name,Value pairs to specify the line width, marker size, and marker colors. Set
the marker edge color to blue and set the marker face color using an RGB color value.
x = -pi:pi/10:pi;
y = tan(sin(x)) - sin(tan(x));
figure
plot(x,y,'--gs',...
'LineWidth',2,...
'MarkerSize',10,...
'MarkerEdgeColor','b',...
'MarkerFaceColor',[0.5,0.5,0.5])
Add Title and Axis Labels
Try This Example
Use the linspace function to define x as a vector of 150 values between 0 and 10. Define y as
cosine values of x.
x = linspace(0,10,150);
y = cos(5*x);
Create a 2-D line plot of the cosine curve. Change the line color to a shade of blue-green using
an RGB color value. Add a title and axis labels to the graph using the title, xlabel,
and ylabel functions.
figure
plot(x,y,'Color',[0,0.7,0.9])
Define t as seven linearly spaced duration values between 0 and 3 minutes. Plot random data
and specify the format of the duration tick marks using the 'DurationTickFormat' name-value
pair argument.
t = 0:seconds(30):minutes(3);
y = rand(1,7);
plot(t,y,'DurationTickFormat','mm:ss')
Specify Axes for Line Plot
Try This Example
% Top plot
ax1 = nexttile;
plot(ax1,x,y1)
title(ax1,'Top Plot')
ylabel(ax1,'sin(5x)')
% Bottom plot
ax2 = nexttile;
plot(ax2,x,y2)
title(ax2,'Bottom Plot')
ylabel(ax2,'sin(15x)')
Modify Lines After Creation
Try This Example
Define x as 100 linearly spaced values between −2π and 2π. Define y1 and y2 as sine and
cosine values of x. Create a line plot of both sets of data and return the two chart lines in p.
x = linspace(-2*pi,2*pi);
y1 = sin(x);
y2 = cos(x);
p = plot(x,y1,x,y2);
Change the line width of the first line to 2. Add star markers to the second line. Use dot notation
to set properties.
p(1).LineWidth = 2;
p(2).Marker = '*';
Plot Circle
Try This Example
Plot a circle centered at the point (4,3) with a radius equal to 2. Use axis equal to use equal data
units along each coordinate direction.
r = 2;
xc = 4;
yc = 3;
theta = linspace(0,2*pi);
x = r*cos(theta) + xc;
y = r*sin(theta) + yc;
plot(x,y)
axis equal
Input Arguments
collapse all
Y—y values
scalar | vector | matrix
y values, specified as a scalar, a vector, or a matrix. To plot against specific x values you must
also specify X.
Data
Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | categorical |
datetime | duration
X—x values
scalar | vector | matrix
x values, specified as a scalar, a vector, or a matrix.
Data
Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | categorical |
datetime | duration
LineSpec — Line style, marker, and color
character vector | string
Line style, marker, and color, specified as a character vector or string containing symbols. The
symbols can appear in any order. You do not need to specify all three characteristics (line style,
marker, and color). For example, if you omit the line style and specify the marker, then the plot
shows only the marker and no line.
Example: '--or' is a red dashed line with circle markers
Line Style Description
-- Dashed line
: Dotted line
-. Dash-dot line
Marker Description
o Circle
+ Plus sign
* Asterisk
. Point
x Cross
s Square
d Diamond
^ Upward-pointing triangle
v Downward-pointing triangle
p Pentagram
h Hexagram
Color Description
y yellow
m magenta
c cyan
r red
g green
b blue
w white
k black
ax — Target axes
Axes object | PolarAxes object | GeographicAxes object
Target axes, specified as an Axes object, a PolarAxes object, or a GeographicAxes object. If
you do not specify the axes and if the current axes are Cartesian axes, then the plot function
uses the current axes. To plot into polar axes, specify the PolarAxes object as the first input
argument or use the polarplot function. To plot into a geographic axes, specify
the GeographicAxes object as the first input argument or use the geoplot function.
Name-Value Pair Arguments
Specify optional comma-separated pairs of Name,Value arguments. Name is the argument
name and Value is the corresponding value. Name must appear inside quotes. You can specify
several name and value pair arguments in any order as Name1,Value1,...,NameN,ValueN.
Example: 'Marker','o','MarkerFaceColor','red'
The chart line properties listed here are only a subset. For a complete list, see Line Properties.
'Color' — Line color
[0 0.4470 0.7410] (default) | RGB triplet | hexadecimal color code | 'r' | 'g' | 'b' | ...
Line color, specified as an RGB triplet, a hexadecimal color code, a color name, or a short
name.
For a custom color, specify an RGB triplet or a hexadecimal color code.
• An RGB triplet is a three-element row vector whose elements specify the intensities of the red,
green, and blue components of the color. The intensities must be in the range [0,1]; for
example, [0.4 0.6 0.7].
• A hexadecimal color code is a character vector or a string scalar that starts with a hash symbol
(#) followed by three or six hexadecimal digits, which can range from 0 to F. The values are
not case sensitive. Thus, the color codes '#FF8800', '#ff8800', '#F80', and '#f80' are equivalent.
Alternatively, you can specify some common colors by name. This table lists the named color
options, the equivalent RGB triplets, and hexadecimal color codes.
Example: 'blue'
Example: [0 0 1]
Example: '#0000FF'
'LineStyle' — Line style
'-' (default) | '--' | ':' | '-.' | 'none'
Line style, specified as one of the options listed in this table.
Value Description
'o' Circle
'*' Asterisk
'.' Point
'x' Cross
'none' No markers
Example: 'Marker','+'
Example: 'Marker','diamond'
'MarkerIndices' — Indices of data points at which to display markers
1:length(YData) (default) | vector of positive integers | scalar positive integer
Indices of data points at which to display markers, specified as a vector of positive integers. If
you do not specify the indices, then MATLAB displays a marker at every data point.
Note
To see the markers, you must also specify a marker symbol.
Example: plot(x,y,'-o','MarkerIndices',[1 5 10]) displays a circle marker at the first, fifth, and
tenth data points.
Example: plot(x,y,'-x','MarkerIndices',1:3:length(y)) displays a cross marker every three data
points.
Example: plot(x,y,'Marker','square','MarkerIndices',5) displays one square marker at the fifth
data point.
'MarkerEdgeColor' — Marker outline color
'auto' (default) | RGB triplet | hexadecimal color code | 'r' | 'g' | 'b' | ...
Marker outline color, specified as 'auto', an RGB triplet, a hexadecimal color code, a color
name, or a short name. The default value of 'auto' uses the same color as the Color property.
For a custom color, specify an RGB triplet or a hexadecimal color code.
• An RGB triplet is a three-element row vector whose elements specify the intensities of the red,
green, and blue components of the color. The intensities must be in the range [0,1]; for
example, [0.4 0.6 0.7].
• A hexadecimal color code is a character vector or a string scalar that starts with a hash symbol
(#) followed by three or six hexadecimal digits, which can range from 0 to F. The values are
not case sensitive. Thus, the color codes '#FF8800', '#ff8800', '#F80', and '#f80' are equivalent.
Alternatively, you can specify some common colors by name. This table lists the named color
options, the equivalent RGB triplets, and hexadecimal color codes.
'yyyy-MM-dd' 2014-04-19
'dd/MM/yyyy' 19/04/2014
'dd.MM.yyyy' 19.04.2014
For a complete list of valid letter identifiers, see the Format property for datetime arrays.
DatetimeTickFormat is not a chart line property. You must set the tick format using the name-
value pair argument when creating a plot. Alternatively, set the format using
the xtickformat and ytickformat functions.
The TickLabelFormat property of the datetime ruler stores the format.
'DurationTickFormat' — Format for duration tick labels
character vector | string
Format for duration tick labels, specified as the comma-separated pair consisting
of 'DurationTickFormat' and a character vector or string containing a duration format.
If you do not specify a value for 'DurationTickFormat', then plot automatically optimizes and
updates the tick labels based on the axis limits.
To display a duration as a single number that includes a fractional part, for example, 1.234
hours, specify one of the values in this table.
Value of DurationTickFormat Description