Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 31

Summary: Visualizing Data in 2D and 3D

Identifying Available Plot Types

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.

x = ["hello" "sweet";"peaceful" "world"]


x =
2×2 string array
"hello" "sweet"
"peaceful" "world"

You can use markup in your labels.


ylabel("\pi r^2")
xticks
Sets tick locations along the x-axis.
xticklabels
Labels the x-axis ticks.
xtickangle
Rotates the x-axis tick labels.
Customizing Plot Properties

Specifying Property Values

plot(x,y,linespec,Property1,Value1,Property2,Value2,Property3,Value3,...)

See the complete list of line specifications


here: https://www.mathworks.com/help/matlab/ref/plot.html#btzitot_sep_mw_3a76f056-2882-44d7-
8e73-c695c0c54ca8.

Common line properties to modify:


 "LineWidth" (width of the line and marker edges)
 "MarkerSize" (size of the marker symbols)
 "MarkerEdgeColor" (color of the edge of the marker symbols)
 "MarkerFaceColor" (color of the interior of the marker symbols)
 "Color" (color of the line, particularly when given as RGB values)
MATLAB Line Properties reference
Specifying Colors

red ("r") green ("g") blue ("b") black ("k")

magenta ("m") yellow ("y") cyan ("c") white ("w")

Or as a vector [R G B] where each value is from 0 to 1.


Axis Control
Get Axes Limits

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

You can use visualization functions to plot your three-dimensional data.

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

The surf function surf(z)


plots z(j,k) over the
point x=k and y=j
x = 11:15;
y = 21:25;
surf(x,y,z)

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.

To ensure text is imported as a string, set the TextType property to string.

data = readtable("dataFile.txt",...
"TextType","string")

EPL = readtable("EPLresults.csv","TextType","string")

Summary: Tables of Data


Storing Data in a Table

The readtable function EPL =


creates a table in MATLAB readtable("EPLresults.xlsx","TextType","string")
from a data file. ;

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

stats = array2table(wdl, ...


"VariableNames",["Wins" "Draws" "Losses"])
The array2table function
stats =
can convert a numeric array
to a table.
Wins Draws Losses
The VariableNames propert
____ _____ ______
y can be specified as a string
array of names to include as
20 11 7
variable names in the table.
12 14 12
23 12 3
19 9 10

Sorting Table Data

The sortrows function sorts the data in EPL = sortrows(EPL,"HomeWins");


ascending order, by default.
Use the optional "descend" parameter to EPL =
sort the list in descending order. sortrows(EPL,"HomeWins","descend");
You can also sort on multiple variables, in EPL = sortrows(EPL,["HomeWins"
order, by specifying a string array of "AwayWins"],"descend");
variable names.
You can also show summary statistics for summary(EPL)
variables in a table.

Extracting Portions of a Table

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

EPL.TW = EPL.HW + EPL.AW


EPL =
Team HW HD
HL AW AD AL TW
___________________ __ __
__ __ __ __ __
You can also use dot notation to create "Leicester City" 12 6
new variables in a table. 1 11 6 2 23
"Arsenal" 12 4
3 8 7 4 20
"Manchester City" 12 2
5 7 7 5 19
"Manchester United" 12 5
2 7 4 8 19

draws = EPL{:,["HD" "AD"]}


draws =
If you want to extract multiple variables, 6 6
you can do this using curly braces. 4 7
2 7
5 4

Specify row indices to extract specific


rows. draws13 = EPL{[1 3],["HD" "AD"]}
draws =
6 6
2 7

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.

Summary: Preprocessing Data


Normalizing Data

normalize
Normalize data using a specified normalization method.

Normalize the columns of a matrix using z- xNorm = normalize(x)


scores.
Center the mean of the columns in a matrix on xNorm =
zero. normalize(x,"center","mean")
Scale the columns of a matrix by the first xNorm =
element of each column. normalize(x,"scale","first")
Stretch or compress the data in each column of xNorm = normalize(x,"range",[a
a matrix into a specified interval. b])

Working with Missing Data


Any calculation involving NaN results in NaN. There are three ways to work around this, each with
advantages and disadvantages:

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

Ignores NaNs by default Includes NaNs by default


(default flag is "omitnan") (default flag is "includenan")
max cov
min mean
median
std
var

Data Type Meaning of "Missing"


double NaN
single

string array Empty string (<missing>)


datetime NaT
duration NaN
calendarDuration
categorical <undefined>
Interpolating Missing Data

fillmissing
Fills missing values of an array or table.

Interpolation assuming equal z = fillmissing(y,"method")


spacing of observations.
Interpolation with given observation z =
locations. fillmissing(y,"method","SamplePoints",x)

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)).

n Degree of fitted polynomial.


Inputs

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:

p(x) = -0.1296 x3 + 0.6865 x2 - 0.1759 x + 1.6746


Evaluate the polynomial

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.

xFit Location of points (independent variable values) at


which to evaluate the polynomial.

Inputs

You can xFit = -1:0.01:6;


evaluate the
polynomial
at any
arbitrary
values of x.

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;

If your graphics object is an array, you can access different b = bar(mat);


elements with indexing. bar2 = b(2);
Set a property for every element in an array of graphics set(b,"FaceAlpha",.5)
objects.
Get the current axes. ax = gca;
One advantage to using graphics objects is that you can scroll through the

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.

Review - Images and 3-D Surface Plots


data = readtable("my3Ddata.csv")
Images or 3-D x y z
plots generally _________ ________ ___________
begin with x, y,
and z data. In
2.2506 -0.30105 0.012974
many cases, the x
-1.3443 -0.79976 -0.11638
and y data are not
0.53421 -0.92891 0.16945
evenly spaced on
-0.070088 -0.67461 -0.044245
a grid.
... ... ...
To interpolate the xvec = -2:.2:2;
data onto a grid, yvec = -2:.05:2;
start by defining
the grid points.
Here, yvec is
denser than xvec.
The meshgrid fun [xgrid,ygrid] = meshgrid(xvec,yvec);
ction will convert
your vectors into
the grid expected
by surf and pcol
or.
Then use zgrid = griddata(data.x,data.y,data.z,xgrid,ygrid);
the griddata func
tion to interpolate
your data onto the
grid.

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.

You can also im = pcolor(xgrid,ygrid,zgrid);


visualize your 3-D im.EdgeAlpha = 0;
data as an
pseudocolor
image.
This scaled image imagesc(xvec,yvec,zgrid);
contains the same
data, but the first
two inputs are the
vectors of grid
points instead of
the output
from meshgrid.

If you inspect the


right yellow shape,
you can see that
the imagesc plot is
vertically flipped
from
the pcolor plot.
You can access the current figure and axes with gcf and gca. To move between levels of the hierarchy, use
the Children and Parent properties.
Review - Defining Categories of Data
Categories and Sets

Categorical arrays use less less memory x = categorical(["medium" "large"


and work with many plotting functions. "large" "red" "small" "red"]);
c = categories(x)
c =
4×1 cell array
Use the categories function to get a list
of unique categories. {'large' }
{'medium'}
{'red' }
{'small' }
x = mergecats(x,["small" "medium"
"large"],"size")
x =
Merge different categories with
1×6 categorical array
the mergecats function.
size size size red
size red
x = renamecats(x,"red","color")
x =
Rename categories with 1×6 categorical array
the renamecats function.
size size size
color size color

Discretizing Continuous Data


Ranges in continuous data can represent categories. Categorize continues data into discrete bins with
the discretize function.
>>
y
= discretize(
X
,
edges
,
"Categorical",cats
)
y If the "Categorical" option is set, y is a categorical array. Otherwise, y is numeric bin
values.
Outputs
X Array of continuous data. X is usually numeric or datetime.
edges Consecutive elements in edges form discrete bins. There will be one fewer bins
than the number of edges specified.

You can use Inf in edges to create a bin with no edge.


"Categorical",cat Optional input for the name of each bin category.
s
Inputs

You might also like