Professional Documents
Culture Documents
Summary
Summary
Function Description
scatter Scatter plot, with variable marker size and color
bar Bar graph (vertical and horizontal)
stem Discrete sequence (signal) plot
stairs Stairstep graph
area Filled area plot
pie Pie chart
histogram Histogram
>> scatter(
x
,
y
,
n
,
c
,
filled
)
x x-data
y y-data
n marker size
c color
filled If provided, markers will be filled in disks. Otherwise, they are circles.
Inputs
See the complete list of all available plots here.
Customizing Annotations
Arrays of strings are useful for annotating visualizations. Use square brackets, [], with spaces and
semicolons, ; to create a string array the same way you create a numeric matrix.
plot(x,y,linespec,Property1,Value1,Property2,Value2,Property3,Value3,...)
v = axis
v =
0 12 0.1 0.9
Custom Axis Limits
xlim([-1 13])
ylim([-1 2])
Axis Limits = Data Range
axis tight
Plotting Multiple Columns
You can use the plot function on a matrix to plot each column as a separate line in your plot.
Visualizing Matrices
z
z =
0 0 0 0 0
z is a 5-by-5 matrix 0 0 -6 0 0
0 -3 1 3 0
0 0 8 1 0
0 0 0 0 0
To
specify x and y coordinate
s, you can pass them in as
vectors. Here,
The number
of elements
of x must
match the
number of
columns
of z
The number
of elements
of y must
match the
number of
rows of z
Exporting a Figure
You can either copy and paste output or export a figure as an image file.
There are several ways to store text in MATLAB. Strings (created with double quotation marks) are
the easiest to use in most cases.
data = readtable("dataFile.txt",...
"TextType","string")
EPL = readtable("EPLresults.csv","TextType","string")
teamWinsTable = table(team,wins)
teamWins =
Team Wins
The table function can
___________________ ____
create a table from
workspace variables.
"Arsenal" 20
"Chelsea" 12
"Leicester City" 23
"Manchester United" 19
EPL
EPL =
Team HW
HD HL AW AD AL
___________________ __
__ __ __ __ __
"Leicester City" 12
6 1 11 6 2
"Arsenal" 12
4 3 8 7 4
Display the original table. "Manchester City" 12
2 5 7 7 5
"Manchester United" 12
5 2 7 4 8
"Chelsea" 5
9 5 7 5 7
"Bournemouth" 5
5 9 6 4 9
"Aston Villa" 2
5 12 1 3 15
EPL(2:4,[1 2 5])
ans =
Team HW
AW
___________________ __
__
Inside parenthesis, specify the row numbers of "Arsenal" 12
the observations and column numbers of the 8
table variables you would like to select. "Manchester City" 12
7
"Manchester United" 12
7
You may also use the name of the variable for EPL(2:4,["Team" "HW" "AW"])
indexing. ans =
Team HW
If you want to reference more than one variable, AW
use a string array containing the variable names. ___________________ __
__
"Arsenal" 12
8
"Manchester City" 12
7
"Manchester United" 12
7
Extracting Data from a Table
EPL
EPL =
Team HW HD
HL AW AD AL
___________________ __ __
__ __ __ __
"Leicester City" 12 6
Display the original table.
1 11 6 2
"Arsenal" 12 4
3 8 7 4
"Manchester City" 12 2
5 7 7 5
"Manchester United" 12 5
2 7 4 8
tw = EPL.HW + EPL.AW
tw =
You can use dot notation to extract 23
data for use in calculations or plotting. 20
19
19
Exporting Tables
You can use the writetable function to create a file from a table.
writetable(tableName,"myFile.txt","Delimiter","\t")
The file format is based on the file extension, such as .txt, .csv, or .xlsx , but you can also specify
a delimiter.
writetable
Write a table to a file.
normalize
Normalize data using a specified normalization method.
Ignore missing data when performing Maintains the integrity of the data but can be difficult to
calculations. implement for involved calculations.
Remove missing data. Simple but, to keep observations aligned, must remove
entire rows of the matrix where any data is missing,
resulting in a loss of valid data.
Replace missing data. Keeps the data aligned and makes further computation
straightforward, but modifies the data to include values
that were not actually measured or observed.
The Clean Missing Data task can be used to remove or interpolate missing data. You can add one to
a script by selecting it from the Live Editor tab in the toolstrip.
Data contains missing values, in the form of both - x = [2 NaN 5 3 -999 4 NaN];
999 and NaN.
ismissing(x)
ans =
The ismissing function identifies only
1×7 logical array
the NaN elements by default.
0 1 0 0 0 0 1
ismissing(x,[-999,NaN])
ans =
Specifying the set of missing values ensures
1×7 logical array
that ismissing identifies all the missing elements.
0 1 0 0 1 0 1
xNaN =
standardizeMissing(x,-999)
Use the standardizeMissing function to convert all xNaN =
missing values to NaN. 2 NaN 5 3
NaN 4 NaN
cleanX = rmmissing(xNaN)
Use the rmmissing function to remove missing cleanX =
values. 2 5 3 4
fillmissing
Fills missing values of an array or table.
Method Meaning
"next" The missing value is the same as the next nonmissing value in the data.
"previous" The missing value is the same as the previous nonmissing value in the data.
"nearest" The missing value is the same as the nearest (next or previous) nonmissing value in
the data.
"linear" The missing value is the linear interpolation (average) of the previous and next
nonmissing values.
"spline" Cubic spline interpolation matches the derivatives of the individual interpolants at the
data points. This results in an interpolant that is smooth across the whole data set.
However, this can also introduce spurious oscillations in the interpolant between data
points.
"pchip" The cubic Hermite interpolating polynomial method forces the interpolant to maintain
the same monotonicity as the data. This prevents oscillation between data points.
Polynomial Fitting
Determine the coefficients
You can use the function polyfit to compute the coefficients of a least-squares polynomial fit to
the data.
>>
c
= polyfit(
x
,
y
,
n
)
c Vector of polynomial coefficients.
Outputs
x Location of data points (independent variable
values).
y Data to fit polynomial to (y = p(x)).
x = 0:5;
y = [2 1 4 4 3 2];
plot(x,y)
Suppose
that you
have two
vectors x an
d y.
Fit a c = polyfit(x,y,3)
polynomial c =
of degree 3
to the x- -0.1296 0.6865 -0.1759 1.6746
y data.
Coefficients in the output vector are ordered from the highest to the lowest degree. So, the polynomial
which fits the x-y data can be expressed as:
Given the vector c containing the coefficients of the polynomial, you can evaluate the polynomial at
any value of x using the polyval function.
>>
yFit
= polyval(
c
,
xFit
)
yFi Value of the polynomial at the given points (yFit = p(xFit)).
t
Outputs
c Vector of polynomial coefficients.
Inputs
A common
approach is
to create a
vector of
uniformly
spaced x val
ues.
Evaluate xFit = -1:0.01:6;
and plot the yFit = polyval(c,xFit);
fitted hold on
polynomial plot(xFit,yFit)
at values hold off
contained in
the
vector xFit.
Review - Graphics Formatting Functions
Scatter Plots
Scatter plots differ from line plots in that the data points are not connected, and there are more
options to customize the individual markers.
>> scatter(
x
,
y
,
sz
,
col
,
"filled"
)
x x-data
y y-data
sz The size of the markers can be a constant or vector specifying the size of each point individually.
col The color of the markers can be a constant or vector specifying the color of each point
individually. In the latter case, colors are picked from a color map.
"filled Provide this optional fifth input to specify that the markers be filled in.
"
Inputs
You can use the command colorbar to include a bar that indicates the value of the colors
represented in the color map. Use the Name,Value pair, "MarkerFaceAlpha",a to add transparency
to the markers.
Bar Plots
Create bar(vec)
a bar
plot
from a
vector.
Chang bar(vec,"FaceColor","r")
e the
color
of a
bar
plot.
Chang colors = [0 0 1; 1 0 0; 1 0 0; 1 0 0];
e the bar(world,"FaceColor","flat","CData",colors)
colors
of
individ
ual
bars.
Create mat = [1 2 3; 4 5 6];
bar bar(mat)
charts
for
each
row of
a
matrix.
bar(mat')
Histogram
Create histogram(vec,"BinWidth",50)
a
histogr
am
with a
specifie
d bin
width.
Create histogram(vec,15)
a
histogr
am
with a
specifie
d
number
of bars.
Review - Customizing Graphics Objects
Different types of visualizations and graphics objects have different properties, which you can extract,
use, and update. You can get a handle to the graphics object by assigning the output of a plotting
function to a variable.
If you know in advance you want to edit the figure, you can f = figure;
create one before plotting.
You can create figure handles to graphics objects when you p = p(x,y);
create the plot.
p.LineStyle = ":";
Access and modify properties with dot notation.
p.XData(end+1) = 1;
Add data to a plot. This can be used for creating animations. p.YData(end+1) = 2;
properties.
All graphics objects are part of a hierarchy. Most graphics objects consist of a figure window,
containing one or more axes, which contain any number of plot objects.
Consistent naming
of your variables
from previous
steps will
the griddata synt
ax easier.
surf(xgrid,ygrid,zgrid);
Once your x, y,
and z data is
gridded, you can
visualize it in a
variety of
ways. surf create
s a surface plot.
Notice the
difference between
the x and y axes.
This is
because xvec and
yvec had a
different number of
grid points.