Discrete Data Plots
Discrete Data Plots
Visualize discrete data using plots such as bar graphs or stem plots. For example, you can create a vertical or
horizontal bar graph where the bar lengths are proportional to the values that they represent.
Bar graph
Syntax
bar(y)
bar(x,y)
bar(___,width)
bar(___,style)
bar(___,color)
Description
bar(y) creates a bar graph with one bar for each element in y.
bar(___,width) sets the relative bar width, which controls the separation of bars within a group.
bar(___,style) specifies the style of the bar groups. For example, use 'stacked' to display each group as
one multicolored bar.
bar(___,color) sets the color for all the bars. For example, use 'r' for red bars.Create Bar Graph
Example:
Example:
y = [75 91 105 123.5 131 150 179 203 226 249 281.5];
bar(y)
1
Specify Bar Locations
x = 1900:10:2000;
y = [75 91 105 123.5 131 150 179 203 226 249 281.5];
bar(x,y)
Example: Set the width of each bar to 40 percent of the total space available for each bar.
y = [75 91 105 123.5 131 150 179 203 226 249 281.5];
bar(y,0.4)
2
Display Groups of Bars
y = [2 2 3; 2 5 6; 2 8 9; 2 11 12];
bar(y)
Syntax
barh(y)
barh(x,y)
barh(___,width)
barh(___,style)
barh(___,color)
Description
3
barh(y) creates a horizontal bar graph with one bar for each element in y. If y is an m-by-n matrix, then barh
creates m groups of n bars.
barh(x,y) draws the bars along the vertical axis at the locations specified by x.
barh(___,width) specifies the fraction of available space occupied by each bar. For example, barh(y,1) makes
the bars in each group touch each other. Specify width as the last argument in any of the previous syntaxes.
barh(___,style) specifies the style of the bar groups. For example, barh(y,'stacked') stacks the bars within each
group into a multicolored bar.
barh(___,color) specifies a single color for all the bars. For example, barh(y,'red') displays red bars.
Example: Create a vector of four values. Display the values in a bar graph with one horizontal bar for each
value.
y = [10 20 30 41];
barh(y)
Example: Define x as a matrix of three year values. Define y as a matrix containing snowfall data for four cities.
Display the four series of bars in groups for each year. Then add the axis labels and a legend.
4
3-D bar graph
Syntax
bar3(z)
bar3(y,z)
bar3(___,width)
bar3(___,style)
bar3(___,color)
Description
bar3(z) creates a 3-D bar graph for the elements of z. Each bar corresponds to an element in z.
• To plot a single series of bars, specify z as a vector. For a vector of length m, the function plots the bars
on a y-axis ranging from 1 to m.
• To plot multiple series of bars, specify z as a matrix with one column for each series. For an m-by-n
matrix, the function plots the bars on an x-axis ranging from 1 to n and a y-axis ranging from 1 to m.
bar3(y,z) creates a bar graph of the elements in z at the y-values specified in y. If z is a matrix, elements from
the same row in z appear at the same location along the y-axis.
bar3(___,width) sets the width of the bars along the x- and y-axes and controls the separation of bars within a
group. By default, width is 0.8 and the bars have a slight separation. If width is 1, the bars within a group touch
one another.
bar3(___,style) specifies the style of the bars, where style can be 'detached', 'grouped', or 'stacked'. The default
mode of display is 'detached'.
bar3(___,color) displays all bars using the color specified by color. For example, use 'r' to specify all red bars.
5
Example: Specify z as a vector of five values. Plot these values as a series of 3-D bars, with the height of each
bar corresponding to a value in z and its y-axis location corresponding to the index of that value.
z = [50 40 30 20 10];
bar3(z);
Example: Specify z as a matrix. Create a 3-D bar graph of z with each series corresponding to a column in z.
z = [1 4 7; 2 5 8; 3 6 9; 4 7 10];
bar3(z)
Example: Specify y as a vector of y-axis locations for the bars in z. Plot the bars at the specified y-axis
locations.
6
bar3(y,z)
Example: Specify z as a matrix with three series. Plot z with the data for each row grouped together by using
the 'grouped' style. To eliminate space between bars of the same group, set width to 1.
Example: Create a set of y-coordinates ranging from 0 to π. Plot sine functions of the y-values as a 3-D bar
graph. Use the color specification 'r' to make the bars red.
y = 0:pi/16:pi;
z = [sin(y')/4 sin(y')/2 sin(y')];
bar3(y,z,1,'r')
7
Scatter plot
Syntax
scatter(x,y)
scatter(x,y,sz)
scatter(x,y,sz,c)
Description
scatter(x,y) creates a scatter plot with circular markers at the locations specified by the vectors x and y.
scatter(x,y,sz) specifies the circle sizes. To use the same size for all the circles, specify sz as a scalar. To plot
each circle with a different size, specify sz as a vector or a matrix.
scatter(x,y,sz,c) specifies the circle colors. You can specify one color for all the circles, or you can vary the color.
For example, you can plot all red circles by specifying c as "red".
Example: Create x as 200 equally spaced values between 0 and 3π. Create y as cosine values with random
noise. Then, create a scatter plot.
x = linspace(0,3*pi,200);
y = cos(x) + rand(1,200);
scatter(x,y)
8
Vary Circle Size
Example: Create a scatter plot using circles with different sizes. Specify the size in points squared
x = linspace(0,3*pi,200);
y = cos(x) + rand(1,200);
sz = linspace(1,100,200);
scatter(x,y,sz)
Corresponding elements in x, y, and sz determine the location and size of each circle. To plot all circles with the
equal area, specify sz as a numeric scalar.
x = linspace(0,3*pi,200);
y = cos(x) + rand(1,200);
c = linspace(1,10,length(x));
scatter(x,y,[],c)
9
Corresponding elements in x, y, and c determine the location and color of each circle. The scatter function maps
the elements in c to colors in the current colormap.
Example: Named color palettes provide a convenient way to change the colors of a chart. This example
compares a scatter plot with three different color palettes.
x = rand(50,5);
y = randn(50,5) + (5:5:25);
scatter(x,y,"filled")
Stairstep graph
Syntax
stairs(Y)
stairs(X,Y)
10
stairs(___,LineSpec)
Description
stairs(X,Y) plots the elements in Y at the locations specified by X. The inputs X and Y must be vectors or
matrices of the same size. Additionally, X can be a row or column vector and Y must be a matrix with length(X)
rows.
stairs(___,LineSpec) specifies a line style, marker symbol, and color. For example, ":*r" specifies a dotted red
line with asterisk markers. Use this option with any of the input argument combinations in the previous syntaxes.
Example: Create a stairstep plot of sine evaluated at 40 equally spaced values between 0 and 4π.
X = linspace(0,4*pi,40);
Y = sin(X);
figure
stairs(Y)
Example: Create a stairstep plot of two cosine functions evaluated at 50 equally spaced values between 0 and
4π.
X = linspace(0,4*pi,50)';
Y = [0.5*cos(X), 2*cos(X)];
figure
stairs(Y)
11
12