Matlab Plot commands
•MATLAB provides an extensive suite of plotting functions and customization
options, empowering users to create a diverse range of visualizations.
• plotting in Matlab have following advantages −
• Line plots, scatter plots, bar graphs, histograms, surface plots, and more cater to
different data types and analysis needs in Matlab.
• User have control over colors, markers, line styles, axes properties, and
annotations that allow tailoring plots to specific requirements.
• MATLAB’s plots can be made interactive, allowing zooming, panning, and data
inspection for deeper exploration.
3.
•
Matlab Plot -There are various functions for plotting data in MATLAB. Plot visualized in
window named ‘Figure’. Various common graphics functions available are:
4.
2-D line plot
Variouscommand formats
to plot vector and Matrix
data
• plot(X,Y)
• plot(X,Y,LineSpec)
• plot(X1,Y1,...,Xn,Yn)
• plot(X1,Y1,LineSpec1,...,
Xn,Yn,LineSpecn)
• plot(Y)
• plot(Y,LineSpec)
• plot(X,Y) creates a 2-D line plot of the data in Y versus the corresponding values in X.
• To plot a set of coordinates connected by line segments, specify X and Y as vectors of the same
length.
• To plot multiple sets of coordinates on the same set of axes, specify at least one of X or Y as a
matrix.
• plot(X,Y,LineSpec) creates the plot using the specified line style, marker, and color.
• plot(X1,Y1,...,Xn,Yn) plots multiple pairs of x- and y-coordinates on the same set of axes.
• plot(X1,Y1,LineSpec1,...,Xn,Yn,LineSpecn) assigns specific line styles, markers, and colors to each
x-y pair.
• You can specify LineSpec for some x-y pairs and omit it for others. For example,
plot(X1,Y1,"o",X2,Y2) specifies markers for the first x-y pair but not for the second pair.
• plot(Y) plots Y against an implicit set of x-coordinates.
• If Y is a vector, the x-coordinates range from 1 to length(Y).
• If Y is a matrix, the plot contains one line for each column in Y. The x-coordinates range from 1 to
the number of rows in Y.
• If Y contains complex numbers, MATLAB® plots the imaginary part of Y versus the real part of Y.
• plot(Y,LineSpec) plots Y using implicit x-coordinates, and specifies the line style, marker, and
color.
5.
2-D Plot
• Variouscommand formats to plot Table
data
• plot(tbl,xvar,yvar)
• plot(tbl,yvar)
• Other options:
• plot(ax,___)
• plot(___,Name,Value)
• p = plot(___)
• Table Data
• plot(tbl,xvar,yvar) plots the variables xvar and
yvar from the table tbl.
• To plot one data set, specify one variable for
xvar and one variable for yvar.
• To plot multiple data sets, specify multiple
variables for xvar, yvar, or both.
• If both arguments specify multiple variables,
they must specify the same number of
variables. (since R2022a)
• plot(tbl,yvar) plots the specified variable
from the table against the row indices of the
table.
6.
2-D Plot :single line
• script file:
• Define x as a vector of linearly spaced values
between 0 and 2π. Use an increment of π/100
between the values.
• Define 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)
7.
2-D Plot :Multiple lines
• Plot Multiple Lines
• 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)
8.
2-D Plot :for Matrix data
• Define Y as the 4-by-4 matrix. Create a 2-D line plot of Y.
• y = magic(4)
• figure
• plot(y)
• 16 2 3 13
• 5 11 10 8
• 9 7 6 12
• 4 14 15 1
• Note: MATLAB® plots each matrix column as a separate
line. MATLAB® cycles the line color through the default
color order.
9.
2-D Plot :Line specifications
• Specify Line Style
• 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.
10.
2-D Plot :Line specifications
• 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.
• 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*')
11.
2-D Plot :Line specifications
• Display Markers at Specific Data Points
• Create a line plot and display markers at every
fifth data point by specifying a marker symbol
and setting the MarkerIndices property
• x = linspace(0,10);
• y = sin(x);
• plot(x,y,'-o','MarkerIndices',1:5:length(y))
12.
LineSpec — Linestyle, marker, and color
Line style, marker, and color, specified as a string scalar or
character vector containing symbols.
The symbols can appear in any order.
do not need to specify all three characteristics (line style,
marker, and color).
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.
"-" (default)
LineWidth — Line width
0.5 (default) | positive value
Line width, specified as a positive value in points,
where 1 point = 1/72 of an inch.
If the line has markers, then the line width also
affects the marker edges.
Marker — Markersymbol
"none" (default) | "o" | "+"
| "*" | "." | ...
Marker symbol, specified as
one of the values listed in
this table.
By default, the object does
not display markers.
Specifying a marker symbol
adds markers at each data
point or vertex.
MarkerSize — Marker size
6 (default) | positive value
Marker size, specified as a
positive value in points,
where 1 point = 1/72 of an
inch.
‘S’
‘D’
‘P’
‘H’
15.
• MarkerEdgeColor
• Markeroutline 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 string scalar or
character vector 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. Therefore,
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.
16.
• MarkerFaceColor —Marker fill color
• Marker fill color, specified as "auto", an RGB triplet, a
hexadecimal color code, a color name, or a short name.
• The "auto" option uses the same color as the Color
property of the parent axes. If you specify "auto" and the
axes plot box is invisible, the marker fill color is the color
of the figure.
• 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 string scalar or character
vector 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. Therefore, 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.
17.
2-D Plot :Line specifications
• 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])
18.
2-D Plot :Add Title and Axis Labels
• Use the linspace function to define x as a vector of 150 values
between 0 and 10.
• Define y as cosine values of 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.
• x = linspace(0,10,150);
• y = cos(5*x);
• figure
• plot(x,y,'Color',[0,0.7,0.9])
• title('2-D Line Plot')
• xlabel('x')
• ylabel('cos(5x)')
19.
2-D Plot :Modify Lines After Creation
• 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 = '*';
20.
Plot Circle
• Plota circle centered at the point (4,3) with a
radius equal to 2.
• Use ‘axis equal ’ to use equal scale factor
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
21.
2-D Plot :Table values
• A convenient way to plot data from a table is to pass the table to the plot function and specify the variables to plot.
• Read weather.csv as a table tbl.
• Then display the first three rows of the table.
• tbl = readtimetable("weather.csv");
• tbl = sortrows(tbl);
• head(tbl,3)
• Time WindDirection WindSpeed Humidity Temperature RainInchesPerMinute CumulativeRainfall PressureHg PowerLevel
LightIntensity
• ____________________ _____________ _________ ________ ___________ ___________________ __________________
__________ __________ ______________
• 25-Oct-2021 00:00:09 46 1 84 49.2 0 0 29.96 4.14 0
• 25-Oct-2021 00:01:09 45 1.6 84 49.2 0 0 29.96 4.139 0
• 25-Oct-2021 00:02:09 36 2.2 84 49.2 0 0 29.96 4.138 0
• Plot the row times on the x-axis and the RainInchesPerMinute variable on the y-axis.
• p = plot(tbl,"RainInchesPerMinute");
22.
• To modifyaspects of the line, set the LineStyle, Color,
and Marker properties on the Line object.
• For example, change the line to a red dotted line with
point markers.
• p.LineStyle = ":";
• p.Color = "red";
• p.Marker = ".";