matlab 画图


先前讲解了简单绘图方法: http://www.cnblogs.com/youxin/p/3859923.html
x = 0:pi/100:2*pi;
y = sin(x);
plot(x,y)
下面例子:

This example shows how to create a simple line graph. Use the linspace function to define x as a vector of 100 linearly spaced values between 0 and .
x = linspace(0,2*pi,100);

Define y as the sine function evaluated at the values in x.
y = sin(x);

Plot y versus 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 x and y.
x = linspace(0,2*pi,25);
y = sin(x);

Create a stairstep plot of y versus x. Open a new figure window using the figure command. 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)

Plot Multiple Lines

This example shows how to plot more than one line by passing multiple x,y pairs to the plot function.

Define y1 and y2 as 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)

plot cycles 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,y pair. 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,y pair.

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

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

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,y pair.

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') g--也行。

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 plot adds a marker to each data point.

Define x as 25 linearly spaced values between 0 and $2pi$ . 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*'.

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 x and 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,'*')

----------------------------------------------------------------------

Create Simple Line Plot

Define x as 100 linearly spaced values between $-2pi$ and $2pi$ . Define y1 and y2 as sine and cosine values ofx. Create a line plot of both sets of data.

x = linspace(-2*pi,2*pi,100);
y1 = sin(x);
y2 = cos(x);

figure
plot(x,y1,x,y2)

Add Title

Add a title to the graph using the title function. Pass the title function a text string with the desired title.

To display Greek symbols in a title, use the TeX markup. Use the TeX markup, pi, to display the Greek symbol $pi$ .

title('Graph of Sine and Cosine Between -2pi and 2pi')

Add Axis Labels

Add axis labels to the graph using the xlabel and ylabel functions. Pass these functions a text string with the desired label.

xlabel('-2pi < x < 2pi') % x-axis label
ylabel('sine and cosine values') % y-axis label

Add Legend

Add a legend to the graph identifying each data set using the legend function. Pass the legend function a text string description for each line. Specify legend descriptions in the order that you plot the lines.

指定lengend根据你画线的顺序。

legend('y = sin(x)','y = cos(x)')

Change Legend Location

Change the location of the legend on the graph by setting its location using one of the eight cardinal or intercardinal directions. Display the legend at the bottom left corner of the axes by specifying its location as'southwest'.

To display the legend outside the axes, append outside to any of the directions, for example,'southwestoutside'.

legend('y = sin(x)','y = cos(x)','Location','southwest')

Change Axis Limits of Graph

This example shows how to change the axis limits of a graph. By default, MATLAB® chooses axis limits to encompass the data plotted.

encompass:包围,环绕。

Create Simple Line Plot

Define x as 200 linearly spaced values between -10 and 10. Define y as the sine of x with an exponentially decreasing amplitude. Create a line plot of the data.

x = linspace(-10,10,200);
y = sin(4*x)./exp(x);

figure
plot(x,y)

Change Axis Limits

Change the axis limits by passing to the axis function a four-element vector of the form[xmin,xmax,ymin,ymax], where xmin and xmax set the scaling for the x-axis, and ymin and ymax set the scaling for the y-axis.

You also can change the axis limits using the xlimylim, and zlim functions. The commandsxlim([xmin,xmax]) and ylim([ymin,ymax]) produce the same result as axis([xmin,xmax,ymin,ymax]).

Change the x-axis scaling to range from 0 to 10. Change the y-axis scaling to range from -1 to 1.

axis([0,10,-1,1])

Use Semiautomatic Axis Limits

Use an automatically calculated minimum x-axis limit by settings its value to -inf. MATLAB® calculates the limit based on the data. Specify values for the maximum x-axis limit and the y-axis limits.

axis([-inf,10,-1,1])

To use an automatically calculated maximum limit, set the value to inf.

-------------

Change Tick Marks and Tick Labels of Graph

This example shows how to change the tick marks and the tick mark labels. MATLAB® chooses tick mark locations based on the range of the data and automatically uses numeric labels at each tick mark.

reate Simple Line Chart

Define x as 200 linearly spaced values between -10 and 10. Define y as the cosine of x. Plot the data.

x = linspace(-10,10,200);
y = cos(x);

figure
plot(x,y)

Change Tick Marks

Change the location of the tick marks on the plot by setting the XTick and YTick properties of the axes. Usegca to set the properties for the current axes. Define the tick marks as a vector of increasing values. The values do not need to be equally spaced.

set(gca,'XTick',[-3*pi,-2*pi,-pi,0,pi,2*pi,3*pi])
set(gca,'YTick',[-1,-0.5,0,0.5,1]) 改变横坐标和纵坐标的tick mark。

Change Tick Mark Labels

Specify tick mark labels by setting the XTickLabel and YTickLabel properties of the axes. Set these properties using a cell array of strings with the desired labels. If you do not specify enough text labels for all the tick marks, then MATLAB cycles through the labels.

set(gca,'XTickLabel',{'-3pi','-2pi','-pi','0','pi','2pi','3pi'})
set(gca,'YTickLabel',{'min = -1','-0.5','0','0.5','max = 1'})

----------------------------------------------------

Create Figure With Multiple Graphs Using Subplots

This example shows how to create a figure containing multiple graphs using the subplot function. The syntax, subplot(m,n,p), divides the figure into an m-by-n grid with an axes in the pth grid location. The grids are numbered along each row.

Create Subplots and Add Subplot Titles

Use subplot to create a figure containing a 2-by-2 grid of graphs. Plot a sine wave in the first subplot.

x = linspace(-5,5); % define x
y1 = sin(x); % define y1

figure % create new figure
subplot(2,2,1) % first subplot
plot(x,y1)
title('First subplot')

Plot another sine wave in the second subplot.

y2 = sin(2*x); % define y2

subplot(2,2,2) % second subplot
plot(x,y2)
title('Second subplot')

Plot two more sine waves in the third and fourth subplots.

y3 = sin(4*x); % define y3
y4 = sin(6*x); % define y4

subplot(2,2,3) % third subplot
plot(x,y3)
title('Third subplot')

subplot(2,2,4) % fourth subplot
plot(x,y4)
title('Fourth subplot')

Add Subplot Axis Labels

Add subplot labels using the xlabel and ylabel functions. By default, xlabel and ylabel label the current axes. The current axes is typically the last axes created or clicked with the mouse. Reissuing the command,subplot(m,n,p), makes the pth subplot the current axes.

Make the third subplot the current axes. Then, label its x-axis and y-axis.

subplot(2,2,3)
xlabel('x-axis')
ylabel('y-axis')

The figure contains four axes with a sine wave plotted in each axes.

更多:
http://www.mathworks.cn/cn/help/matlab/creating_plots/plotting-with-two-y-axes.html
原文地址:https://www.cnblogs.com/youxin/p/3860380.html