Main Content

Create Common 2-D Plots

This example shows how to create a variety of 2-D plots in MATLAB®.

Line Plots

Theplotfunction creates simple line plots ofxandyvalues.

x = 0:0.05:5; y = sin(x.^2); figure plot(x,y)

Figure contains an axes object. The axes object contains an object of type line.

Line plots can display multiple sets ofxandydata.

y1 = sin(x.^2); y2 = cos(x.^2); plot(x,y1,x,y2)

Figure contains an axes object. The axes object contains 2 objects of type line.

Bar Plots

Thebarfunction creates vertical bar charts. Thebarhfunction creates horizontal bar charts.

x = -2.9:0.2:2.9; y = exp(-x.*x); bar(x,y)

Figure contains an axes object. The axes object contains an object of type bar.

Stairstep Plots

Thestairsfunction creates a stairstep plot. It can create a stairstep plot of Y values only or a stairstep plot ofxandyvalues.

x = 0:0.25:10; y = sin(x); stairs(x,y)

Figure contains an axes object. The axes object contains an object of type stair.

Errorbar情节

Theerrorbarfunction draws a line plot ofxandyvalues and superimposes a vertical error bar on each observation. To specify the size of the error bar, pass an additional input argument to theerrorbarfunction.

x = -2:0.1:2; y = erf(x); eb = rand(size(x))/7; errorbar(x,y,eb)

Figure contains an axes object. The axes object contains an object of type errorbar.

Polar Plots

Thepolarplotfunction draws a polar plot of the angle values intheta(在radians) versus the radius values inrho.

theta = 0:0.01:2*pi; rho = abs(sin(2*theta).*cos(2*theta)); polarplot(theta,rho)

Figure contains an axes object. The axes object contains an object of type line.

Stem Plots

Thestemfunction draws a marker for eachxandyvalue with a vertical line connected to a common baseline.

x = 0:0.1:4; y = sin(x.^2).*exp(-x); stem(x,y)

Figure contains an axes object. The axes object contains an object of type stem.

Scatter Plots

Thescatterfunction draws a scatter plot ofxandyvalues.

loadpatientsHeightWeightSystolic散射(身高、体重)包含('Height') ylabel('Weight')

Figure contains an axes object. The axes object contains an object of type scatter.

Use optional arguments to thescatterfunction to specify the marker size and color. Use thecolorbarfunction to show the color scale on the current axes.

scatter(Height,Weight,20,Systolic) xlabel('Height') ylabel('Weight') colorbar

Figure contains an axes object. The axes object contains an object of type scatter.

Related Topics