Vous êtes sur la page 1sur 5

8/28/2015 Create 2-D Graph and Customize Lines - MATLAB & Simulink - MathWorks India

Create 2-D Graph and Customize Lines

Create 2-D Line Graph


This example shows how to create a simple line graph. Use the linspacefunction to define xas a vector of 100 linearly spaced values between 0 and .

x = linspace(0,2*pi,100);

Define yas the sine function evaluated at the values in x.

y = sin(x);

Plot yversus the corresponding values in x.

figure
plot(x,y)

Create Graph in New Figure Window


This example shows how to create a graph in a new figure window, instead of plotting into the current figure.

Define xand y.

x = linspace(0,2*pi,25);
y = sin(x);

Create a stairstep plot of yversus x. Open a new figure window using the figurecommand. If you do not open a new figure window, then by default, MATLAB® clears existing
graphs and plots into the current figure.

figure % new figure window


stairs(x,y)

http://in.mathworks.com/help/matlab/creating_plots/using-high-level-plotting-functions.html 1/5
8/28/2015 Create 2-D Graph and Customize Lines - MATLAB & Simulink - MathWorks India

Plot Multiple Lines


This example shows how to plot more than one line by passing multiple x,ypairs to the plotfunction.

Define y1and y2as sine waves with a phase shift.

x = linspace(0,2*pi,100);
y1 = sin(x);
y2 = sin(x-pi/4);

Plot the lines.

figure
plot(x,y1,x,y2)

plotcycles through a predefined list of line colors.

Colors, Line Styles, and Markers


To change the line color, line style, and marker type, add a line specification string to the x,ypair. For example, adding the string, 'g:*', plots a green dotted line with star
markers. You can omit one or more options from the line specification, such as 'g:'for a green dotted line with no markers. To change just the line style, specify only a line
style option, such as '--'for a dashed line.

For more information, see LineSpec (Line Specification).

Specify Line Style


This example shows how to create a plot using a dashed line. Add the optional line specification string, '--', to the x,ypair.

x = linspace(0,2*pi,100);
y = sin(x);

figure
plot(x,y,'--')

http://in.mathworks.com/help/matlab/creating_plots/using-high-level-plotting-functions.html 2/5
8/28/2015 Create 2-D Graph and Customize Lines - MATLAB & Simulink - MathWorks India

Specify Different Line Styles for Multiple Lines


This example shows how to plot two sine waves with different line styles by adding a line specification string to each x,ypair.

Plot the first sine wave with a dashed line using '--'. Plot the second sine wave with a dotted line using ':'.

x = linspace(0,2*pi,100);
y1 = sin(x);
y2 = sin(x-pi/4);

figure
plot(x,y1,'--',x,y2,':')

Specify Line Style and Color


This example shows how to specify the line styles and line colors for a plot.

Plot a sine wave with a green dashed line using '--g'. Plot a second sine wave with a red dotted line using ':r'. The elements of the line specification strings can appear in
any order.

x = linspace(0,2*pi,100);
y1 = sin(x);
y2 = sin(x-pi/4);

figure
plot(x,y1,'--g',x,y2,':r')

Specify Line Style, Color, and Markers


This example shows how to specify the line style, color, and markers for two sine waves. If you specify a marker type, then plotadds a marker to each data point.

Define xas 25 linearly spaced values between 0 and . Plot the first sine wave with a green dashed line and circle markers using '--go'. Plot the second sine wave with a
red dotted line and star markers using ':r*'.

http://in.mathworks.com/help/matlab/creating_plots/using-high-level-plotting-functions.html 3/5
8/28/2015 Create 2-D Graph and Customize Lines - MATLAB & Simulink - MathWorks India
x = linspace(0,2*pi,25);
y1 = sin(x);
y2 = sin(x-pi/4);

figure
plot(x,y1,'--go',x,y2,':r*')

Plot Only Data Points


This example shows how to plot only the data points by omitting the line style option from the line specification string.

Define the data xand y. Plot the data and display a star marker at each data point.

x = linspace(0,2*pi,25);
y = sin(x);

figure
plot(x,y,'*')

See Also
contour| LineSpec (Line Specification)| linspace| loglog| plot| plotyy| scatter| semilogx| semilogy| stairs| stem

Related Examples
• Add Title, Axis Labels, and Legend to Graph
• Change Axis Limits of Graph
• Change Tick Marks and Tick Labels of Graph

http://in.mathworks.com/help/matlab/creating_plots/using-high-level-plotting-functions.html 4/5
8/28/2015 Create 2-D Graph and Customize Lines - MATLAB & Simulink - MathWorks India

http://in.mathworks.com/help/matlab/creating_plots/using-high-level-plotting-functions.html 5/5

Vous aimerez peut-être aussi