This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English version of the page.

Note: This page has been translated by MathWorks. Click here to see
To view all translated materials including this page, select Country from the country navigator on the bottom of this page.

Combining Different Visualizations

This example shows how to create complex visualizations using multiple chart types or using overlays of the same chart type.

You can create many types of plots using MATLAB®. You also can combine plot types to make interesting visualizations.

Create a Line Plot with Confidence Bounds

Create a plot with confidence bounds using the fill function to draw the confidence bounds and the plot function to draw the data points. Use dot notation syntax object.PropertyName to customize the look of the fill plot.

x = 0:0.2:10;                     % create data for the line plot
y = besselj(0, x);
xconf = [x x(end:-1:1)] ;         % create data for the confidence bounds
yconf = [y+0.15 y(end:-1:1)-0.15];

fi = fill(xconf,yconf,'red');
fi.FaceColor = [1 0.8 0.8];       % make the filled area pink
fi.EdgeColor = 'none';            % remove the line around the filled area
hold on

hold off

Add Error Bars to a Bar Chart

Create a bar chart with error bars by using the bar and errorbar functions together.

data = [37.6 24.5 14.6 18.1 19.5 8.1 28.5 7.9 3.3 4.1 7.9 1.9 4.3]';
errhigh = [2.1 4.4 0.4 3.3 2.5 0.4 1.6 0.8 0.6 0.8 2.2 0.9 1.5];
errlow  = [4.4 2.4 2.3 0.5 1.6 1.5 4.5 1.5 0.4 1.2 1.3 0.8 1.9];

bar(data, 'FaceColor', 'cyan')                 % create the bar chart
hold on

er = errorbar(1:13, data, errlow, errhigh);    % create the error bars
er.Color = [0 0 0];                            % make the errorbars black
er.LineStyle = 'none';                         % remove the line connecting the errorbars
hold off

Create a Contour Plot with Vector Lines

Use the contourf function with the quiver function to produce a contour plot with vector lines.

x = -3:.2:3;                    
y = -3:.2:3;
[X,Y] = meshgrid(x,y);      % create a grid of X and Y points
z = peaks(X,Y);             % use peaks to create a 3-D surface

[c,h] = contourf(x,y,z);    % draw a contour plot
axis([-3 3 -3 3])
hold on

[px,py] = gradient(z);      % calculate the gradient at each point
quiver(x,y,px,py,2,'k')     % draw a quiver plot of the gradients
hold off

Change the Color of a Single Bar

You can create a bar chart where one bar is highlighted in a different color by splitting the data and creating two overlapping bar charts.

For example, create a bar chart where the seventh bar is red. First, create a bar chart and replace the data for the seventh bar with NaN. Then, overlay a second bar chart in red using only the data for the seventh bar.

data = [37.6 24.5 14.6 18.1 19.5 8.1 28.5 7.9 3.3 4.1 7.9 1.9 4.3]';

data1 = data;
data1(7) = NaN;
bar(data1, 'FaceColor', 'blue')     % create the first bar chart in blue
hold on

data2 = NaN(1,13);
data2(7) = data(7);
bar(data2, 'FaceColor', 'red')      % create the second bar chart in red
hold off

Create a Contour Plot with Major and Minor Contour Lines

You can create a contour plot with emphasis on selected contour lines by splitting the data and creating two overlapping contour plots.

For example, create a contour plot of the peaks function where the even numbered contours lines are solid and the odd numbered contour lines are dotted. Plot one contour for the even numbered levels. Then, overlay a second contour plot with the odd numbered levels drawn with a dotted line.

major = -6:2:8;
minor = -5:2:7;
[cmajor,hmajor] = contour(peaks,'LevelList',major);    % contour with major (even-numbered) levels
clabel(cmajor,hmajor)                                  % label the major levels

hold on
[cminor,hminor] = contour(peaks,'LevelList',minor);    % contour with minor (odd-numbered) levels
hminor.LineStyle = ':';                                % make the minor levels dotted
hold off

Was this topic helpful?