二维绘图

Click here to load reader

Upload: odysseus-marks

Post on 01-Jan-2016

42 views

Category:

Documents


0 download

DESCRIPTION

二维绘图. 一、绘制 2d 图形的基本步骤 二、直角坐标系绘图: plot( ), plotyy( ),… 三、图形控制: figure,axis,subplot,… 四、二维绘图函数汇总. 1 、 手工作图步骤. 一、 绘制 2d 图形的基本步骤. 如何画出 y = sin(x) 在 [0, 2*pi] 上的图像?. (1) 找点: x=0, pi/6, pi/3, pi/2, 2*pi/3, 5*pi/6, …. (2) 计算函数值: y=sin(0), sin(pi/6), sin(pi/3), …. (3) 描点:在坐标系中画出这些离散点. - PowerPoint PPT Presentation

TRANSCRIPT

  • 2dplot( ), plotyy( ),figure,axis,subplot,

  • 2d 1(1) x=0, pi/6, pi/3, pi/2, 2*pi/3, 5*pi/6, (2) y=sin(0), sin(pi/6), sin(pi/3), (3)(4) ysin(x) [0, 2*pi]

  • 2Matlab (1)x=[0:pi/10:2*pi]y=sin(x)(2) plot(x,y) (3)grid onaxis([-1,8,-2,2])

  • 3matlabx = [0,4/3,1,1/2,1/3,2/3,1/6,1,5/6,4/3,7/6,5/3,3/2,2,11/6]x = x*piy = sin(x)plot(x,y)

    x = [0,4/3,1,1/2,1/3,2/3,1/6,1,5/6,4/3,7/6,5/3,3/2,2,11/6]x = sort(x)*piy = sin(x)plot(x,y)

  • 4Matlab

  • 1plot( )2fplot( )3ezplot( )4stairs( )5yplotyy( ) 6loglog( ) semilogx( )semilogy( )

  • 1plot( )(Figure)plot(Y) plot(X1,Y1,...,Xn,Yn) plot(X1,Y1,LineSpec,...,Xn,Yn,LineSpec) plot(...,'PropertyName',PropertyValue,...)h = plot( . . . ) h

  • plot(X,Y)X, Y X Y X, Y X, Y X Y X, Y X, Y plot(real(X),real(Y))

  • x Y x Y x Y x Y plot(x,Y) x , Y plot(X,y) X , y y X X y y X X y

  • plot(Y)x = 1:length(Y)plot(x,Y)x = 1:sizeY,1)plot(x,Y)YY Y plot(real(Y),imag(Y))

  • plot(X1,Y1,LineSpec,...,Xn,Yn,LineSpec) Xn, Yn, LineSpecLineSpecXn,Yn,LineSpecXn,Ynplot(X1,Y1,X2,Y2,LineSpec,X3,Y3)LineSpec

    - : -. -- . o x + s d v ^ < > p h b g r c m y k w

  • x = 0:0.2:2*piy = sin(x)plot(x,y,'r-o')x = 0:0.2:2*piy = sin(x)plot(x,y,'ro')plot( )

  • plot(X1,Y1,LineSpec,PropertyName,PropertyValue) plotproperties Color[r,g,b]r,g,b01LineWidthLineStyleMarkerMarkerEdgeColor marker( filled markers MarkerFaceColorthe face of filled markers MarkerSizePropertyName

  • x = -pi:pi/10:pi;y = tan(sin(x)) - sin(tan(x));plot(x,y,'--rs','LineWidth',2,... 'MarkerEdgeColor','k',... 'MarkerFaceColor','g',... 'MarkerSize',10)

  • h = plot( . . . ) h get( ) set( )x = 0:0.2:2*piY = [sin(x); 2*sin(2*x)]h = plot(x,Y)get(h(1))set(h(1),'Color','r','LineWidth',1,'Marker','+')set(h(2),'Color','g','LineWidth',2,'Marker','s')

  • 2fplot( )fplot(fun,limits) limitsfunfplot(fun,limits,LineSpec)LineSpec[X,Y] = fplot(fun,limits,...)fun y=f(x) 12x sin(x), diric(x,10), '[sin(x),cos(x)]' 3@sin,@coslimits1x[xmin xmax]2xy[xmin xmax ymin ymax]

  • %fplot('sin',[-pi,pi],'o')fplot( ) 1fplot( )

  • %x%fplot('sin(x)+cos(x)',[-pi,pi])fplot( ) 2%x%fplot('[sin(x), cos(x)]',[-pi,pi])

  • fplot( ) 3f1 = @sin%%fplot(f1,[-pi,pi,-0.3,0.6])

    f2 = @(x)cos(tan(x))+tan(cos(x)) %fplot(f2,[-pi,pi])[xmin xmax ymin ymax]

  • ezplot(f)3ezplotezplot(f,[a,b])ezplot(f(x,y))ezplot(f(x,y),[a,b,c,d])ezplot(f,g,[a,b])ezplot(f,g) ezplot('x^2 - y^2 = 1')h = ezplot(f,g)

  • 4yplotyy( )plotyy(X1,Y1,X2,Y2)X1,Y1X2,Y2x1,y1x2,y2plotyy(X1,Y1,X2,Y2,'function')functionfunctionplot, semilogx, semilogy, loglog, stemplotyy(X1,Y1,X2,Y2,'function1','function2')function1X1, Y1function2X2, Y2[AX,H1,H2] = plotyy(...)AXYAX(1)YAX(2)YH1H2plotyy( )

  • plotyy( )plot( )

  • plotyyx = 0:0.01:20;y1 = 200*exp(-0.05*x).*sin(x);y2 = 0.8*exp(-0.5*x).*sin(10*x);[AX,H1,H2] = plotyy(x,y1,x,y2,'plot');

    title('Multiple Decay Rates') xlabel('Time (\musec)')

    set(get(AX(1),'Ylabel'),'String','Slow Decay') set(get(AX(2),'Ylabel'),'String','Fast Decay')

    set(H1,'LineStyle','--')set(H2,'LineStyle',':')

  • 5loglog(Y)loglog(X1,Y1,...)loglog(X1,Y1,LineSpec,...)loglog(...,'PropertyName',PropertyValue,...)h = loglog(...)loglog( ) xysemilogx( ) xysemilogy( ) xyplot( )

  • x = logspace(-1,2); loglog(x,exp(x),'-s') grid on x = logspace(-1,2); plot(x,exp(x),'-s') grid on loglog( ) plot( )

  • x = logspace(-1,2); semilogx(x,exp(x),'-s') grid on x = logspace(-1,2); semilogy(x,exp(x),'-s') grid on semilogx( ) semilogy( )

  • 1Matlab2figure,close,3subplot4axes,xlim,5hold66title, xlabel, text, ...

  • Matlab1Matlab

  • ( handle )matlab

  • get set delete(h) hgcf Get Current Figuregca Get Current Axesgco Get Current Objectf = get(h,parent);l = get(h,children);

  • ( Root ) Root MatlabRootFigureMatlabRootRootRoot 0 root

    CurrentFigurefigurechildrenUnitspixelinchesScreenSize[left,bottom,width,height]docsearch root properties root

  • root ScreenSize ss = get(0,'ScreenSize') ss = [ left, bottom, width, height ]ScreenSize( pixel )(1,1)inches(0,0)

  • 2FigureMatlabFigurefigure( )close( )clf( ) gcf

  • figurefigure figure(PropertyName,propertyvalue,...) figure(h) hhh (h >= 1)hh h=figure(...)

  • 1closefigureclose(gcf)

    2close(h)hfigurehh

    3close namefigure

    4close all figure

    5close all hiddenfigure

    6status = close(...) figure10close

  • clf1clf(HandleVisibilityon) 2. clf(reset) clf reset(Position, Units, PaperPosition, PaperUnits) 3clf(fig) clf(fig,reset) fig 4figure_handle = clf(...)

  • Figure

    Color[r g b]CurrentAxesCurrentObjectHandleVisibility{on} | callback | offNumberTitlefigure on | off NameNextPlotnew | {add} | replace | replacechildrenPosition[ left, bottom, width, height ]Resize on | off Toolbar/ none | figure | auto MenuBar/ none | figure | auto TagWindowStyle normal | modal | docked Visible on | off docsearch figure properties figure

  • figure Position Position [ left, bottom, width, height ]

  • figurePosition, Color, x = 0:pi/10:2*pi

    ss = get(0,'ScreenSize')W = ss(3);H = ss(4);

    figure(1)set(gcf,'Position',[W/2,H/2,W/3,H/4])set(gcf,'Name','sin(x)','NumberTitle','off')plot(x,sin(x))

    h = figureset(h,'Position',[W/2,H/10,W/3,H/4])set(h,'Name','cos(x)','Color',[1,1,1])set(1,'Toolbar','none','Menubar','none')plot(x,cos(x))

  • figure NextPlot x = 0:0.1:7h = figureplot(x,sin(x))x = 0:0.1:7h = figureset(h,'NextPlot','new')plot(x,sin(x))

    newaddreplacechildrenHandleVisibilityonfigureNextPlot add clfreplaceHandleVisibilityfigure PositionunitsPaperPositionPaperUnits NextPlotadd clf reset

  • 3(subplot)subplot(m,n,p) mnmn p subplot(1,1,1)()subplot

  • subplot(m,n,p,replace) axesaxessubplot(Position,[left bottom width height]) Position subplot(..., prop1, value1, prop2, value2, ...) h = subplot(...)

    h = subplot(m,n,p) p

    subplot(h) h

  • subplot(m,n,P) PPPsubplot(2,3,1)subplot(2,3,2)subplot(2,3,3)subplot(2,3,4)subplot(2,3,5:6)subplot(2,3,1)subplot(2,3,3)subplot(2,3,4)subplot(2,3,6)subplot(2,3,[2,5])subplot(2,3,1)subplot(2,3,4)subplot(2,3,[3,5,6])

  • subplot( ) x = -pi:pi/10:pi; x = x'; Y = [sin(x),cos(x),x.^2,exp(x)]; for k = 1:4 subplot(2,2,k); plot(x,Y(:,k)); endsubplot( )subplotfigure

  • 1mn210.001322x(t)y(t)

    m223-3n3-355

  • 4AxesFigurelineimagepatchsurfacelighttextaxes( ) clagcaaxis( ) xlim( ) / ylim( ) / zlim( )x / y / z gridboxxlabel( ) / ylabel( ) / zlabel( )x / y / z

  • axes figure axes(PropertyName,propertyvalue,...) MATLAB axes(h) hhChildrenCurrentAxeshCurrentObjectset(figure_handle,CurrentAxes,axes_handle), h = axes(...) axes( )

  • axes

    Color[ r g b ]ColorOrder[ r1 g1 b1; r2 g2 b2; ]LineStyleOrder - * | : | o NextPlot add | {replace} | replacechildrenPosition[200 200 1000 400]LineWidthTickDir in | out TickLength[2DLength 3DLength]docsearch axes properties axes

  • axes Position Position [ left, bottom, width, height ] (0,0)(1,1) left, bottom , width, height01

  • axes NextPlot

    addreplacechildrenHandleVisibilityonclareplace HandleVisibility figure position Units cla reset

  • figureh1 = axes('position',[0.08,0.1,0.4,0.4])h2 = axes('position',[0.6,0.1,0.35,0.7])h3 = axes('position',[0.08,0.6,0.4,0.3])set(h1,'Color',[0.5,0.5,0.5],'XColor',[1,0,0],'LineWidth',2)set(h1,'TickDir','out','TickLength',[0.04,0.03])set(h2,'TickLength',[0.06,0.02])

  • Properties That Control the X-, Y-, or Z-Axis

    XColor, YColor, ZColorColor of axis linesXDir, YDir, ZDir{normal} | reverseXGrid, YGrid, ZGridon | {off}XLabel, YLabel, ZLabelhandle of text objectXLim, YLim, ZLim[minimum maximum]XLimMode, YLimMode, ZLimMode{auto} | manualXMinorGrid, YMinorGrid, ZMinorGridon | {off}XMinorTick, YMinorTick, ZMinorTickon | {off}XScale, YScale, ZScale{linear} | logXTick, YTick, ZTickvector of data values locating tick marksXTickLabel, YTickLabel, ZTickLabelstringXTickMode, YTickMode, ZTickMode{auto} | manualXTickLabelMode, YTickLabelMode, ZTickLabelMode{auto} | manual

  • clcclear allclose allfigureaxesset(gca,'fontsize',16)set(gca,'XLim',[0,6])s = get(gca,'XTickLabel')s(1:2:end,:) = char(32)set(gca,'XTickLabel',s)set(gca,'XTick',0:0.5:6)XLim, XTick, XTickLabel

  • claclacla reset axes(Position, Units)cla(ax) cla(ax,'reset') ax

  • axis( )v = axisv vaxis([xmin, xmax, ymin, ymax]) 2daxis([xmin, xmax, ymin, ymax, zmin, zmax]) 3d inf inf axis autoxyzaxis auto x xaxis 'auto yz' yzaxis manualaxis(axis) hold onXLimModeYLimModeZLimModemanual

  • axis tightaxis fillPlotBoxAspectRatio position PlotBoxAspectRatioMode DataAspectRatioMode manual

  • axis equal axis imageaxis equal axis squarexyzaxis normal

  • axis vis3daxis offaxis on

  • axis iji j axis xyxy

  • axis(axes_handles,...) axissquareh1 = subplot(221);h2 = subplot(222);axis([h1 h2],'square')[mode,visibility,direction] = axis('state') .mode ' auto ' | ' manual ' visibility ' on ' | ' off ' direction' xy ' | ' ij ' XLimModeYLimModeZLimModeautomodeauto;XLimModeYLimModeZLimModemanualmodemanual

  • x = 0:.025:pi/2;plot(x,tan(x),'-ro')x = 0:.025:pi/2;plot(x,tan(x),'-ro')axis([0 pi/2 0 5])axis( )

  • xlim( ) / ylim( ) / zlim( )axisxlimylimzlimxlim xxlim([xmin xmax]) xxlim(mode) : x auto manuallim(auto)x auto.xlim('manual') x manual.xlim(axes_handle,...) x

  • 5holdhold onhold on NextPlotadd

    hold allColorOrder LineStyleOrder

    hold offhold off NextPlotreplace

    holdhold on/off

    hold(axes_handle,...) holdisholdholdonoffhold on1axesholdaxes

  • holdx = 0.1:0.5:4y = sqrt(x)plot(x,y,'r')axis([-1,5,0,2.5])hold onplot(x,y,'bo','markersize',10)plot(x,y,'b+','markersize',20)

  • holdplot( ) plot(x([1,2]),y([1,2]))hold onaxis([-1.1,1.1,-1.1,1.1])for k = 2:length(t)-1plot(x([k,k+1]),y([k,k+1]))pause(0.1)end

  • (1) boxbox on box off boxbox(axes_handle,...) 6

  • (2) gridgrid on grid off/ grid grid on / grid offgrid(axes_handle,...) grid minor

  • (3) title(text)

  • xlabel(text) X ylabel(text) Y zlabel(text) Z (4) xlabel( ) / ylabel( ) / zlabel( )

  • (5)legend(string1,string2, ...) x = 0:0.05: 7y1 = log10(x)y2 = sin(x)plot(x,y1,'b',x,y2,'r')axis([0,7,-2,2])legend('Log(x)','Sin(x)')

  • (6) text( )text(x,y,string) (x,y) text(x,y,string,'PropertyName',PropertyValue...)x = 0:0.05: 7y = log10(x)plot(x,y)s = '\leftarrow Log(x)'text(2, 0.2, s, 'fontsize',20)

  • TeX / LaTeXMatlabtext titlexlabelylabelzlabeltextlegendASCII TeX / LaTeX Matlab TeX textInterpreter:

    tex TeX latex LaTeXnone TeXD.E.Knuth LaTeXL.LamportTeX

  • TeX Character Sequence

    \alpha\upsilon\sim~\angle\phi\leq\ast*\chi\infty\beta\psi\clubsuit\gamma\omega\diamondsuit\delta\Gamma\heartsuit\epsilon\Delta\spadesuit\zeta\Theta\leftrightarrow\eta\Lambda\leftarrow\theta\Xi\Leftarrow\vartheta\Pi\uparrow\iota\Sigma\rightarrow\kappa\Upsilon\Rightarrow\lambda\Phi\downarrow\mu\Psi\circ\nu\Omega\pm\xi\forall\geq\pi\exists\propto\rho\ni\partial\sigma\cong\bullet\varsigma\approx\div\tau\Re\neq\equiv\oplus\aleph\Im\cup\wp\otimes\subseteq\oslash\cap\in\supseteq\supset\lceil\subset\int\cdot\o\rfloor\neg\nabla\lfloor\timesx\ldots...\perp\surd\prime\wedge\varpi\0\rceil\rangle\mid|\vee\copyright\langle

  • \bf

    \it

    \sl

    \rm

    \fontname{}\fontname{}

    \fontsize{}\fontsize{16}

    _{} { }

    ^{} { }Tex

  • TeXtitle('{\itAe}^{-\alpha\itt}sin\beta{\itt} \alpha
  • \color{}12redgreenyellowmagentablueblackwhitecyangraybarkGreenorangelightBlue\color{magenta}magenta

    \color[rgb]{a b c}RGB[a b c]abc[0 1]color[rgb]{0 .5 .5}Tex

  • str = '{\color{red}\color{green}\color[rgb]{0 0 1}}'text(0.1,0.5,str,'fontsize',24,'fontname','')TeX

  • \( LaTeX \) $ LaTeX$ $$ LaTeX$$matlabLatex s1 = '$y_1=e^xsin(x)$'s2 = '$$y_2=\frac{1}{2}\sqrt[3]{x}$$'str = char(s1,s2)text(0.2, 0.5, str, 'Interpreter', 'latex', 'FontSize',16)Latexlegend( )Interpreterh = legend(str1, str2, )set(h,'interprete','latex')

  • ^{} _{}TeX \alpha $$\frac{}{}$$ $$\sqrt{x}$$$$\sqrt[n]{x}$$ $$\sum_{i=1}^{n} x_{i}$$$$\int_{0}^{1}$$ $$ \frac { \partial{y} } { \partial{x} } $$$$\lim_{n \rightarrow \infty}$$ %nlim$\lim_{n \rightarrow \infty}$ %nlim$$\overline{x}$$$$\underline{x}$$ %x$$\overbrace{x+y+z+w}$$$$a+\underbrace{b+c+d} $$ LaTeX LaTeX LaTeX

  • Line GraphsBar GraphsArea GraphsDirection GraphsRadial GraphsScatter Graphsplot bar (grouped) area feather polar scatter plotyy barh (grouped) pie quiver rose spy loglog bar (stacked) fill comet compass plotmatrix semilogx barh (stacked) contourf ezpolar semilogy hist image stairs pareto pcolor contour errorbar ezcontourf ezplot stem ezcontour

  • polar(theta,rho) thetarhothetaxrho polar(theta,rho,LineSpec) LineSpec polar(axes_handle,...) axes_handle h = polar(...) h polar( )rho=f(theta), polar( )

  • t = 0:.01:2*pi; polar(t,sin(2*t).*cos(2*t),'--r') t = 0:.01:2*pi; polar(t,sin(3*t).*cos(2*t),'--r')polar( )

  • holdpolar( )hold ont = 0:.01:2*pi;polar(t,sin(3*t).*cos(2*t),'--r')

  • *3