Matlab基础知识四

第四节 绘图

1.plot()

plot(X,Y) creates a 2-D line plot of the data in Y versus the corresponding values in X.

  • If X and Y are both vectors, then they must have equal length. The plot function plots Yversus X.

  • If X and Y are both matrices, then they must have equal size. The plot function plots columns of Y versus columns of X.

  • If one of X or Y is a vector and the other is a matrix, then the matrix must have dimensions such that one of its dimensions equals the vector length. If the number of matrix rows equals the vector length, then the plot function plots each matrix column versus the vector. If the number of matrix columns equals the vector length, then the function plots each matrix row versus the vector. If the matrix is square, then the function plots each column versus the vector.

  • If one of X or Y is a scalar and the other is either a scalar or a vector, then the plotfunction plots discrete points. However, to see the points you must specify a marker symbol, for example, plot(X,Y,'o').

plot(X,Y)为Y中的数据与X中的相应数值创建一个二维线图。
如果X和Y都是向量,那么它们必须有相同的长度。plot函数绘制了Y与X的对比图。
如果X和Y都是矩阵,那么它们必须有相同的大小。plot函数绘制了Y的列与X的列的对比图。
如果X或Y中的一个是矢量,另一个是矩阵,那么矩阵的尺寸必须是其中一个尺寸等于矢量的长度。如果矩阵的行数等于矢量的长度,那么绘图函数就会绘制每个矩阵列与矢量的对比。如果矩阵的列数等于矢量的长度,那么该函数就会绘制每一行与矢量的对比图。如果矩阵是正方形,则函数绘制每一列与矢量的关系。
如果X或Y中的一个是标量,另一个是标量或矢量,那么绘图函数就会绘制出离散的点。然而,为了看到这些点,你必须指定一个标记符号,例如,plot(X,Y,‘o’)。

例1

>> plot(cos(0:pi/20:2*pi));

在这里插入图片描述

>> plot(sin(0:pi/20:2*pi));

在这里插入图片描述

>>  x=0:1:10;y=x.^2+1;
plot(x,y,'or-.');

在这里插入图片描述

matlab画图,一般下两次指令,matlab执行到第二次画图时,会覆盖第一张图,解决办法如下:

hold on/off

添加绘图时,保留当前绘图,不会对原有图形进行覆盖。

>> hold on
plot(cos(0:pi/20:2*pi));
plot(sin(0:pi/20:2*pi));
hold off

在这里插入图片描述

plot(x,y,‘str’)

可以对图形进行更详细的标注,可以对图形的线段、颜色等进行修改

在这里插入图片描述

绘制图形时,可以更改图形的属性

>> plot(cos(0:pi/20:2*pi),'Xg-');

在这里插入图片描述

>> x=[0:1:10];
plot(x,2*x+1,'rp:');

在这里插入图片描述

2.legend()

用Matlab画图时,有时候需要对各种图标进行标注,例如,用“+”代表A的运动情况,“*”代表B的运动情况。 legend函数的基本用法是: legend(string1,string2,string3, …) 分别将字符串1、字符串2、字符串3……标注到图中,每个字符串对应的图标为画图时的图标。

>> x=0:0.5:4*pi;
y=sin(x);h=cos(x);w=1./(1+exp(-x));
g=(1/(2*pi*2)^0.5).*exp((-1.*(x-2*pi).^2)./(2*2^2));
plot(x,y,'bd-',x,h,'gp',x,w,'ro-.',x,g,'c^-');

在这里插入图片描述

加上下面的一行代码

>> legend('sin(x)','cos(x)','Sigmoid','Gauss function');

在这里插入图片描述

3.title()和?label()

绘制图形的标题和坐标轴标签

title()、xlabel()、ylabel()、zlabel()

>> x=0:0.1:2*pi;y1=sin(x);y2=exp(-x);
plot(x,y1,'--*',x,y2,':o');
xlabel('t=0 to 2\pi');
ylabel('values of sin(t) and e^{-x}');
title('Function Plots of sin(t) and e^{-x}');
legend('sin(t)','e^{-x}');

在这里插入图片描述

**注解:**其中xlabel(‘t=0 to 2\pi’) “\pi”代表Π,如果不加“\”,则表示的是Π;e^{-x}特殊字源

4.text()和annotation()

text()函数表示向数据点添加文本说明;

https://blog.csdn.net/jk_101/article/details/110727188

annotation()表示给绘制的图形进行注释。

https://blog.csdn.net/jk_101/article/details/110731532

x=linspace(0,3);y=x.^2.*sin(x);plot(x,y);
line([2,2],[0,2^2*sin(2)]);
str='$$ \int_{0}^{2} x^2\sin(x) dx $$';
text(0.25,2.5,str,'Interpreter','latex');
annotation('arrow','X',[0.32,0.5],'Y',[0.6,0.4]);

在这里插入图片描述

注解:

  1. str=’ ∫ 0 2 x 2 sin ⁡ ( x ) d x \int_{0}^{2} x^2\sin(x) dx 02x2sin(x)dx'; \int 表示积分符号∫,{0}^{2}表示上下标;

  2. text(0.25,2.5,str,‘Interpreter’,‘latex’); 前两个表示位置坐标,第三个表示显示的内容,最后一个固定格式不变。

  3. annotation(‘arrow’,‘X’,[0.32,0.5],‘Y’,[0.6,0.4]);arrow表示箭头,后面表示箭头的位置坐标。

  4. linspace(x1,x2,N)是Matlab中的一个指令,用于产生x1,x2之间的N点行矢量。其中
    x1为起始值
    x2为终始值
    N为元素个数

    若缺省N,默认点数为100。

练习:

>> x=linspace(1,2);y1=x.^2;y2=sin(2*pi*x);
plot(x,y1,'k-',x,y2,'ro');
xlabel('Time(ms)');
ylabel('y(t)');
title('Mini Assignment #1');
legend('t^2','sin(2Πt)','Location','northwest');

在这里插入图片描述

5.Figure Adjustment

图形调整

Font

Font Size

Line width

Axis limit

Tick position

Tick label

>> x=linspace(0,2*pi,1000);y=sin(x);
plot(x,y);set(gcf,'Color',[1 1 1]);

在这里插入图片描述

Figure画布,Axes坐标轴,Line图形的线条

在这里插入图片描述

用get()和set()来添加和修改图形的属性

x=linspace(0,2*pi,1000);
y=sin(x);plot(x,y);
h=plot(x,y);get(h);

AlignVertexCenters: 'off'
            Annotation: [1x1 matlab.graphics.eventdata.Annotation]
          BeingDeleted: 'off'
            BusyAction: 'queue'
         ButtonDownFcn: ''
              Children: [0x0 GraphicsPlaceholder]
              Clipping: 'on'
                 Color: [0 0.4470 0.7410]
             CreateFcn: ''
             DeleteFcn: ''
           DisplayName: ''
      HandleVisibility: 'on'
               HitTest: 'on'
         Interruptible: 'on'
              LineJoin: 'round'
             LineStyle: '-'
             LineWidth: 0.5000
                Marker: 'none'
       MarkerEdgeColor: 'auto'
       MarkerFaceColor: 'none'
            MarkerSize: 6
                Parent: [1x1 Axes]
         PickableParts: 'visible'
              Selected: 'off'
    SelectionHighlight: 'on'
                   Tag: ''
                  Type: 'line'
         UIContextMenu: [0x0 GraphicsPlaceholder]
              UserData: []
               Visible: 'on'
                 XData: [1x1000 double]
             XDataMode: 'manual'
           XDataSource: ''
                 YData: [1x1000 double]
           YDataSource: ''
                 ZData: [1x0 double]
           ZDataSource: ''

get(gca)

                       ALim: [0 1]
                   ALimMode: 'auto'
     ActivePositionProperty: 'outerposition'
          AmbientLightColor: [1 1 1]
               BeingDeleted: 'off'
                        Box: 'on'
                   BoxStyle: 'back'
                 BusyAction: 'queue'
              ButtonDownFcn: ''
                       CLim: [0 1]
                   CLimMode: 'auto'
             CameraPosition: [3.5000 0 17.3205]
         CameraPositionMode: 'auto'
               CameraTarget: [3.5000 0 0]
           CameraTargetMode: 'auto'
             CameraUpVector: [0 1 0]
         CameraUpVectorMode: 'auto'
            CameraViewAngle: 6.6086
        CameraViewAngleMode: 'auto'
                   Children: [1x1 Line]
                   Clipping: 'on'
              ClippingStyle: '3dbox'
                      Color: [1 1 1]
                 ColorOrder: [7x3 double]
            ColorOrderIndex: 2
                  CreateFcn: ''
               CurrentPoint: [2x3 double]
            DataAspectRatio: [3.5000 1 1]
        DataAspectRatioMode: 'auto'
                  DeleteFcn: ''
                  FontAngle: 'normal'
                   FontName: 'Helvetica'
                   FontSize: 10
              FontSmoothing: 'on'
                  FontUnits: 'points'
                 FontWeight: 'normal'
                  GridAlpha: 0.1500
              GridAlphaMode: 'auto'
                  GridColor: [0.1500 0.1500 0.1500]
              GridColorMode: 'auto'
              GridLineStyle: '-'
           HandleVisibility: 'on'
                    HitTest: 'on'
              Interruptible: 'on'
    LabelFontSizeMultiplier: 1.1000
                      Layer: 'bottom'
             LineStyleOrder: '-'
        LineStyleOrderIndex: 1
                  LineWidth: 0.5000
             MinorGridAlpha: 0.2500
         MinorGridAlphaMode: 'auto'
             MinorGridColor: [0.1000 0.1000 0.1000]
         MinorGridColorMode: 'auto'
         MinorGridLineStyle: ':'
                   NextPlot: 'replace'
              OuterPosition: [0 0 1 1]
                     Parent: [1x1 Figure]
              PickableParts: 'visible'
         PlotBoxAspectRatio: [1 0.7882 0.7882]
     PlotBoxAspectRatioMode: 'auto'
                   Position: [0.1300 0.1100 0.7750 0.8150]
                 Projection: 'orthographic'
                   Selected: 'off'
         SelectionHighlight: 'on'
                 SortMethod: 'childorder'
                        Tag: ''
                    TickDir: 'in'
                TickDirMode: 'auto'
       TickLabelInterpreter: 'tex'
                 TickLength: [0.0100 0.0250]
                 TightInset: [0.0524 0.0546 0.0071 0.0210]
                      Title: [1x1 Text]
    TitleFontSizeMultiplier: 1.1000
            TitleFontWeight: 'normal'
                       Type: 'axes'
              UIContextMenu: [0x0 GraphicsPlaceholder]
                      Units: 'normalized'
                   UserData: []
                       View: [0 90]
                    Visible: 'on'
                      XAxis: [1x1 NumericRuler]
              XAxisLocation: 'bottom'
                     XColor: [0.1500 0.1500 0.1500]
                 XColorMode: 'auto'
                       XDir: 'normal'
                      XGrid: 'off'
                     XLabel: [1x1 Text]
                       XLim: [0 7]
                   XLimMode: 'auto'
                 XMinorGrid: 'off'
                 XMinorTick: 'off'
                     XScale: 'linear'
                      XTick: [0 1 2 3 4 5 6 7]
                 XTickLabel: {8x1 cell}
             XTickLabelMode: 'auto'
         XTickLabelRotation: 0
                  XTickMode: 'auto'
                      YAxis: [1x1 NumericRuler]
              YAxisLocation: 'left'
                     YColor: [0.1500 0.1500 0.1500]
                 YColorMode: 'auto'
                       YDir: 'normal'
                      YGrid: 'off'
                     YLabel: [1x1 Text]
                       YLim: [-1 1]
                   YLimMode: 'auto'
                 YMinorGrid: 'off'
                 YMinorTick: 'off'
                     YScale: 'linear'
                      YTick: [1x11 double]
                 YTickLabel: {11x1 cell}
             YTickLabelMode: 'auto'
         YTickLabelRotation: 0
                  YTickMode: 'auto'
                      ZAxis: [1x1 NumericRuler]
                     ZColor: [0.1500 0.1500 0.1500]
                 ZColorMode: 'auto'
                       ZDir: 'normal'
                      ZGrid: 'off'
                     ZLabel: [1x1 Text]
                       ZLim: [-1 1]
                   ZLimMode: 'auto'
                 ZMinorGrid: 'off'
                 ZMinorTick: 'off'
                     ZScale: 'linear'
                      ZTick: [-1 0 1]
                 ZTickLabel: ''
             ZTickLabelMode: 'auto'
         ZTickLabelRotation: 0
                  ZTickMode: 'auto'

练习:

修改某一个值(属性),结果为去掉红色空白部分

在这里插入图片描述

输入下列两行,(二者同样的效果)可以实现上述结果:

set(gca,‘XLim’,[0,2*pi]);
set(gca,‘YLim’,[-1.2,1.2]);

or

xlim([0,2*pi]);
ylim([-1,1]);

改变坐标轴的大小

>> set(gca,'fontSize',25);

在这里插入图片描述

把X轴的取值改为0,Π/2,Π,…

>> set(gca,'XTick',0:pi/2:2*pi);
set(gca,'XtickLabel',0:90:360);

在这里插入图片描述

在这一过程中,Matlab中对于特殊符号会显示不出来,例如π,因为matlab不支持symbol格式,我们可以用string代替symbol,这样就可以把π显示出来了。以下两种形式都可以。

>>set(gca,'FontName','string');
set(gca,'XTickLabel',{'0','π/2','π','3π/2','2π'});
>>set(gca,'FontName','string');
set(gca,'XTickLabel',{'0','\pi/2','\pi','3\pi/2','2\pi'});

Line style and width:

>>x=linspace(0,2*pi,1000);
y=sin(x);plot(x,y);
h=plot(x,y);
set(gca,'FontName','char');
set(gca,'XTickLabel',{'0','π/2','π','3π/2','2π'});
set(h,'LineStyle','-.',...
'LineWidth',7.0,'Color','g');

在这里插入图片描述

如果用plot来输出,去掉h,则图形是什么样的呢?

>>plot(x,y,'-.g',...
'LineWidth',7.0);

在这里插入图片描述

6.Marker Specification

标记点

练习:

>> x=rand(20,1);set(gca,'FontSize',18);
plot(x,'-md','LineWidth',2,'MarkerEdgeColor','K',...
'MarkerFaceColor','g','MarkerSize',10);xlim([1,20]);

在这里插入图片描述

注解:

matlab中的rand函数(用于产生随机数)均匀分布的随机数或矩阵
语法
Y = rand(n)
Y = rand(m,n)
Y = rand([m n])
Y = rand(m,n,p,…)
Y = rand([m n p…])
Y = rand(size(A))
rand
s = rand(‘state’)
描述
rand函数产生由在(0, 1)之间均匀分布的随机数组成的数组。
Y = rand(n) 返回一个nxn的随机矩阵。如果n不是数量,则返回错误信息。
Y = rand(m,n) 或 Y = rand([m n]) 返回一个mxn的随机矩阵。
Y = rand(m,n,p,…) 或 Y = rand([m n p…]) 产生随机数组。
Y = rand(size(A)) 返回一个和A有相同尺寸的随机矩阵。

7.Multiple Figures

如果需要把图分开画,下面代码可以实现:

>> x=-10:0.1:10;
y1=x.^2-8;
y2=exp(x);
figure,plot(x,y1);
figure,plot(x,y2);

在这里插入图片描述
在这里插入图片描述

注意,gcf在修改的过程中,可能不是想要的结果

可以用

figure(‘Position’,[left,bottom,width,height]);

这样修改图式的话,就不容易出错了!!

在这里插入图片描述

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

言沫dl

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值