MATLAB初阶绘图

目录

前言

一、初阶绘图

plot() ​

hold on/off

绘制样式​

legend()  绘制图例

title() 和 label()

text() 和 annotation()

Exercise​

二、图形调整

图形属性

修改图形属性 

获取对象的句柄

获取或修改对象属性

获取线的属性

获取坐标轴(axes)的属性

坐标轴属性

设定坐标轴上下限

设定坐标轴字号和刻度

修改坐标轴字体和刻度标签

图线属性

标记点的属性

Exercise

多个图形

指定图形位置和大小​

在一个图形中绘制多个图形

网格、框和轴的控制

将图形保存到文件中

前言

为在B站跟随台湾大学郭彦甫老师学习matlab的笔记,截图来自郭老师PPT,部分内容参考B站弹幕和评论,仅供学习参考。


一、初阶绘图

plot() MATLAB初阶绘图_第1张图片

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

MATLAB初阶绘图_第2张图片

matlab 绘制会将上一次的绘制覆盖

hold on/off

开始在上一次的图上继续绘制,停止在上一次的图上继续绘制,可实现同一图之中不同函数图像

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

MATLAB初阶绘图_第3张图片

绘制样式MATLAB初阶绘图_第4张图片

hold on
plot(cos(0:pi/20:2*pi),'or--');
plot(sin(0:pi/20:2*pi),'xg:');
hold off

MATLAB初阶绘图_第5张图片

legend()  绘制图例

使用 legeng('L1',...) 绘制图例

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');

MATLAB初阶绘图_第6张图片

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');        %字符pi需要使用反义符号\
ylabel('values of sin(t) and e^{-x}')       %字符幂只用特殊符号
title('Function Plots of sin(t) and e^{-x}');
legend('sin(t)','e^{-x}');

MATLAB初阶绘图_第7张图片

text() 和 annotation()

添加文字和注解

使用 LaTex 的数学表达式文本

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 $$';       %LaTex中字符串形式
text(0.25,2.5,str,'Interpreter','latex');       %绘制积分式
annotation('arrow','X',[0.32,0.5],'Y',[0.6,0.4]);

MATLAB初阶绘图_第8张图片

ExerciseMATLAB初阶绘图_第9张图片

x=linspace(1,2);
f=x.^2;
g=sin(2*pi.*x);
plot(x,f,'b-',x,g,'or');
title('Mini Assignment#1');
legend('t^{2}','sin(2\pit)');
xlabel('Time(ms)');
ylabel('f(t)');

MATLAB初阶绘图_第10张图片

二、图形调整

调整图形的字体、字号、线宽、坐标轴轴上下限、标记位置、标记标签

图形属性MATLAB初阶绘图_第11张图片

MATLAB初阶绘图_第12张图片

修改图形属性 

表示一个对象的句柄

修改一个对象的属性

获取对象的句柄

在绘图时获取

h=plot(x,y);

实用函数MATLAB初阶绘图_第13张图片

获取或修改对象属性

获取用 get()

修改用 set()

获取线的属性

>> x = linspace(0, 2*pi, 1000);
y = sin(x); h = plot(x,y); 
get(h)
    AlignVertexCenters: 'off'
            Annotation: [1×1 matlab.graphics.eventdata.Annotation]
          BeingDeleted: 'off'
            BusyAction: 'queue'
         ButtonDownFcn: ''
              Children: [0×0 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'
         MarkerIndices: [1×1000 uint64]
            MarkerSize: 6
                Parent: [1×1 Axes]
         PickableParts: 'visible'
              Selected: 'off'
    SelectionHighlight: 'on'
                   Tag: ''
                  Type: 'line'
         UIContextMenu: [0×0 GraphicsPlaceholder]
              UserData: []
               Visible: 'on'
                 XData: [1×1000 double]
             XDataMode: 'manual'
           XDataSource: ''
                 YData: [1×1000 double]
           YDataSource: ''
                 ZData: [1×0 double]
           ZDataSource: ''

获取坐标轴(axes)的属性

>> 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: [1×1 Line]
                   Clipping: 'on'
              ClippingStyle: '3dbox'
                      Color: [1 1 1]
                 ColorOrder: [7×3 double]
            ColorOrderIndex: 2
                  CreateFcn: ''
               CurrentPoint: [2×3 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'
                     Legend: [0×0 GraphicsPlaceholder]
             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: [1×1 Figure]
              PickableParts: 'visible'
         PlotBoxAspectRatio: [1 0.7896 0.7896]
     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.0512 0.0540 0.0071 0.0206]
                      Title: [1×1 Text]
    TitleFontSizeMultiplier: 1.1000
            TitleFontWeight: 'normal'
                       Type: 'axes'
              UIContextMenu: [0×0 GraphicsPlaceholder]
                      Units: 'normalized'
                   UserData: []
                       View: [0 90]
                    Visible: 'on'
                      XAxis: [1×1 NumericRuler]
              XAxisLocation: 'bottom'
                     XColor: [0.1500 0.1500 0.1500]
                 XColorMode: 'auto'
                       XDir: 'normal'
                      XGrid: 'off'
                     XLabel: [1×1 Text]
                       XLim: [0 7]
                   XLimMode: 'auto'
                 XMinorGrid: 'off'
                 XMinorTick: 'off'
                     XScale: 'linear'
                      XTick: [0 1 2 3 4 5 6 7]
                 XTickLabel: {8×1 cell}
             XTickLabelMode: 'auto'
         XTickLabelRotation: 0
                  XTickMode: 'auto'
                      YAxis: [1×1 NumericRuler]
              YAxisLocation: 'left'
                     YColor: [0.1500 0.1500 0.1500]
                 YColorMode: 'auto'
                       YDir: 'normal'
                      YGrid: 'off'
                     YLabel: [1×1 Text]
                       YLim: [-1 1]
                   YLimMode: 'auto'
                 YMinorGrid: 'off'
                 YMinorTick: 'off'
                     YScale: 'linear'
                      YTick: [-1 -0.8000 -0.6000 -0.4000 -0.2000 0 0.2000 0.4000 0.6000 0.8000 1]
                 YTickLabel: {11×1 cell}
             YTickLabelMode: 'auto'
         YTickLabelRotation: 0
                  YTickMode: 'auto'
                      ZAxis: [1×1 NumericRuler]
                     ZColor: [0.1500 0.1500 0.1500]
                 ZColorMode: 'auto'
                       ZDir: 'normal'
                      ZGrid: 'off'
                     ZLabel: [1×1 Text]
                       ZLim: [-1 1]
                   ZLimMode: 'auto'
                 ZMinorGrid: 'off'
                 ZMinorTick: 'off'
                     ZScale: 'linear'
                      ZTick: [-1 0 1]
                 ZTickLabel: ''
             ZTickLabelMode: 'auto'
         ZTickLabelRotation: 0
                  ZTickMode: 'auto'

坐标轴属性

设定坐标轴上下限

gca 表示坐标轴

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

MATLAB初阶绘图_第14张图片

使用如下代码也可完成同样操作

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

设定坐标轴字号和刻度

修改坐标轴刻度和刻度标签

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

MATLAB初阶绘图_第15张图片

MATLAB初阶绘图_第16张图片

修改坐标轴字体和刻度标签

>> set(gca, 'FontName', 'tex');
set(gca, 'XTickLabel', {'0', '\pi/2', '\pi', '3\pi/2', '2\pi'});
%\pi为pi在tex字体中的转义

MATLAB初阶绘图_第17张图片

图线属性

修改图线的属性

>> set(h, 'LineStyle', '-.','LineWidth', 7.0, 'Color', 'g');

MATLAB初阶绘图_第18张图片在绘制时可以指定属性

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

标记点的属性

MarkerEdgeColor 标记点边框颜色

MarkerFaceColor 标记点填充色

MarkerSize 标记点大小

'-md' 表示绘制菱形(diamond)标记点的直线(-)品红色(magenta)

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

MATLAB初阶绘图_第19张图片

ExerciseMATLAB初阶绘图_第20张图片

x=linspace(1,2);
f=x.^2;
g=sin(2*pi.*x);
h=plot(x,f,'k-',x,g,'or');
title('Mini Assignment#1');
legend('t^{2}','sin(2\pit)');
xlabel('Time(ms)');
ylabel('f(t)');
set(h,'LineWidth',1.5,'MarkerFaceColor','m','MarkerSize',4);
set(gca,'FontSize',16);

MATLAB初阶绘图_第21张图片

多个图形

通过调用图形创建图形窗口

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

MATLAB初阶绘图_第22张图片使用 gcf 句柄时要小心,gcf只能指向最后绘制的图形

指定图形位置和大小MATLAB初阶绘图_第23张图片

>> figure('Position', [left, bottom, width, height]);

在一个图形中绘制多个图形

subplot()

>> t = 0:0.1:2*pi; x = 3*cos(t); y = sin(t);
subplot(2, 2, 1); plot(x, y); axis normal
subplot(2, 2, 2); plot(x, y); axis square
subplot(2, 2, 3); plot(x, y); axis equal
subplot(2, 2, 4); plot(x, y); axis equal tight

MATLAB初阶绘图_第24张图片

网格、框和轴的控制

grid 网格

box 边框

axis 坐标轴

MATLAB初阶绘图_第25张图片

将图形保存到文件中

saveas(gcf,'

MATLAB初阶绘图_第26张图片

你可能感兴趣的:(MATLAB,matlab)