• Matlab 绘图实例


    概要

     

    每次用 Matlab 绘图都要搜一堆资料设置一些参数,本次将绘图中的一些参数设置实例展示在这里,以备不时之需。暂包括折线图,面积图。

     


    折线图实例

     
    下图是效果图:

    图 1:折线图效果图

     
    Matlab 代码如下:

    clc;clear;
    y1 = 0; y2 = 0;
    len = 249;
    for i = 2:len
        y1(i) = y1(i-1) + unifrnd(-0.8,1); 
    end
    for i = 2:len
        y2(i) = y2(i-1) + unifrnd(-0.9,1); 
    end
    
    fig = figure();
    ax = axes;
    % fig.Visible = 'off';  % 设置图片不可见,即只保存图片到本地
    plot( y1, 'linewidth', 1.75, 'Color', 'red')
    hold on;
    plot(y2, 'linewidth', 1.75, 'Color', [0.27451, 0.5098, 0.70588])
    
    ax.YGrid = 'on'; % 把 gca 当成结构体。R2014b 版本之前用 set(gca, 'YGrid', 'on'),下同
    ax.XColor = 'k'; %set(gca, 'XColor', 'k')
    fig.Position = [10 10 630 300];  %set(gcf,'Position',[left,top,width,height]
    box off; %去掉坐标的边框
    ylabel('random value');
    xlabel('random date');
    xtickangle(0); % x 轴标签旋转
    
    h = legend('随机曲线 y_1', '随机曲线 y_2', 'Location', 'northwest', 'Orientation', 'horizontal');
    h.Box = 'off';   %set(h, 'Box', 'off'); % 去掉图例边框
    title('图 1. 随机曲线示例图', 'FontSize', 10.5, 'FontWeight', 'normal', 'FontName', 'heiti');
    saveas(gcf, 'Fig1.png'); % 保存图片到本地,也可以自定义路径,路径名+图片名即可
    

     

    面积图实例

     

    下图是效果图:

    图 2:面积图效果图

     
    Matlab 代码如下:

    clc;clear;
    x = 0:0.1:6*pi;
    y1 = sin(x); y2 = y1;
    
    y1(y1<0) = 0;
    y2(y2>0) = 0;
    
    fig = figure(); % 不可见的话,加参数 'Visible', 'off' 或者设定 fig.Visible = 'off'
    ax = axes;
    
    h = area([y1',y2'], 'linewidth', 0.05); % 按列绘图的
    h(1).FaceColor = [0.2,0.8,0.2];%[0.27451, 0.5098, 0.70588]; % 指定第一列填充颜色
    h(2).FaceColor = [1, 0.55, 0]; % 指定第二列填充颜色
    h(1).EdgeAlpha = 0; % 将边线设置为透明,0 到 1 之间
    h(2).EdgeAlpha = 0; % 将边线设置为透明,0 到 1 之间
    
    ax.YGrid = 'on'; %set(gca, 'YGrid', 'on');
    ax.YLim = [-1.2, 1.5]; % set(gca, 'YLim', [-1.2, 1.5]); % 设置 Y 轴显示范围
    fig.Position = [10 10 630 300]; %set(gcf,'Position',[10 10 630 300]); %[left,top,width,height]
    box off; %去掉坐标的边框
    ylabel('sin value');
    xlabel('x value');
    
    h = legend('余弦曲线(正)', '余弦曲线(负)', 'Location', 'northwest', 'Orientation', 'horizontal');
    h.Box = 'off'; %set(h, 'Box', 'off'); % 去掉图例边框
    title('图 2. 面积图示例', 'FontSize', 10.5, 'FontWeight', 'normal', 'FontName', 'heiti');
    saveas(gcf, 'Fig2.png'); % 保存图片到本地,也可以自定义路径,路径名+图片名即可
    

     

    日期作 X 轴标签实例

     

    下图是效果图:

    图 3:日期轴效果图

     

    clc;clear;
    y1 = 0; y2 = 0;
    len = 205;
    
    for i = 2:len
        y1(i) = y1(i-1) + unifrnd(-0.8,1); 
    end
    
    x_time = today:today+len-1;  % datenum 格式 
    
    fig = figure('color', [1 1 1]); % 同时设置背景色为白色
    fig.Position = [10 10 630 270]; %[left,top,width,height]
    ax = axes;
    %fig.Visible = 'off';  % 从 R2014b 开始,您可以使用圆点表示法查询和设置属性
    plot(x_time, y1', 'linewidth', 1.75, 'Color', 'red')
    
    %xlim([x_time(1), x_time(end)]);
    
    xDate = linspace(x_time(1), x_time(end), 10); % 指定显示个数
    ax.XTick = xDate;
    datetick('x','yyyymmdd','keepticks');
    
    %xlim([x_time(1), x_time(end)]);
    
    % 这一句消除了 ytick -
    %ax.YAxis.TickLength = [0,0];
    %ax.YAxis.Color = [0.5 0.5 0.5];%'gray';
    %ax.XAxis.Color = [0.5 0.5 0.5];
    
    ax.YGrid = 'on';
    ax.XColor = 'k';
    
    box off; %去掉坐标的边框
    ylabel('random value');
    xlabel('random date');
    xtickangle(30); % x 轴标签旋转
    
    h = legend('随机曲线 y_1', 'Location', 'northwest', 'Orientation', 'horizontal');
    h.Box = 'off'; % 去掉图例边框
    title('图 1. 随机曲线示例图', 'FontSize', 10.5, 'FontWeight', 'normal', 'FontName', 'heiti','Color', [0.5 0.5 0.5]);
    %saveas(gcf, 'Fig1.png'); % 保存图片到本地
    
  • 相关阅读:
    mysql中cast() 和convert()的用法讲解
    li内有span需要右浮的问题
    svn检出项目
    vue中的 ref 和 $refs
    for in 循环 和for循环 for of循环
    setInterval()、clearInterval()、setTimeout()和clearTimeout() js计数器方法(还有第三个参数)
    利用history.pushState()实现页面无刷新更新
    函数isNaN() parseFloat() parseInt() Math对象
    一个关于margin-top的问题
    vue 父子组件之间传参
  • 原文地址:https://www.cnblogs.com/zhoukui/p/10799219.html
Copyright © 2020-2023  润新知