matlab绘图(3):散点折线图-时间序列

  1. 绘图效果
    这个图忘记单位了,但是plot_scatter_line.m函数跑出来是带单位的哈~
    在这里插入图片描述 2. 复制粘贴好plot_scatter_line.m函数:
function plot_scatter_line(time, x_obs, y_sim,plot_info)

    %% 函数测试开始
    % % 输入示例:
    % time=flxdat.datetime_start;%时间数据,datetime 数组
    % x_obs=fluxdat.alb_derived;% 观测值
    % y_sim=moddat.albedo;% 模拟值
    % plot_info=plot_line_info;%画图信息   
    
    % 创建图形窗口及定位
    [width, height] = deal(plot_info.size(1),plot_info.size(2));
    figure('Units','centimeter','Position',[10 5 width height]); 
    [ax_x, ax_y] = deal(plot_info.ax_pos(1),plot_info.ax_pos(2));
    axes('Units','centimeter','Position',[ax_x ax_y width-ax_x-1 height-ax_y-0.5]);
    
    % 绘制观测值的散点图
    scatter(time, x_obs,5, 'MarkerEdgeColor', 'r');
    hold on;
    
    % 绘制模拟值的折线图
    plot(time, y_sim, 'k-', 'LineWidth', 1);

    % 计算时间序列的总长度
    duration_years = ceil(years(time(end) - time(1)));
    if duration_years<=2
       time_interval=3;
    elseif duration_years > 2 && duration_years <= 4
       % 时间序列[3,6],间隔3~6个月
       time_interval=6;
    elseif duration_years > 4 && duration_years <= 10
       % 时间序列大于6年,间隔1年显示1次
       time_interval=12;
   elseif duration_years > 10 
       % 时间序列大于6年,间隔1年显示1次
       time_interval=36;
    end
        
    % 设置横轴为日期格式
    ax = gca;
    ax.XTick = time(1):calmonths(time_interval):time(end);

    % 将最后一个刻度设置为输入序列的最后一个日期
    % 检查最后两个刻度的间距
    date_1=ax.XTick(end);
    date_2=time(end);
    mon_diff=calmonths(between(date_1,date_2, 'months'));

    if mon_diff >= 5
        xticks([ax.XTick, time(end)]);
    end
    
    datetick('x', 'yyyy/mm/dd', 'keepticks');

    % 设置轴标签字体和字号
    set(gca, 'FontName', 'Times New Roman', 'FontSize', 8);

    % 设置纵轴范围和刻度
    ylim(plot_info.range);
    yticks(plot_info.range(1):plot_info.ax_d:plot_info.range(2));

%     % 设置纵轴刻度显示格式为两位小数
%     ytickformat('%.2f');
    % 设置纵轴刻度显示格式为两位小数,但整数不显示小数部分
    ytickformat('%.2g');
  
    % 添加图例,并设置合适的大小和位置
    legendHandle = legend({plot_info.leg1, plot_info.leg2},...
                          'FontName', 'Times New Roman', 'FontSize', 8, ...    
                          'Location', 'southoutside', ...
                          'Box', 'off', 'NumColumns', 2);
   
    % 调整图例大小和位置
    set(legendHandle, 'Position', plot_info.leg_pos); % 你可以根据需要调整位置和大小
    % left = 0.85:图例左下角位于图框左边缘向右 85% 的位置。
    % bottom = 0.7:图例左下角位于图框底部边缘向上 70% 的位置。
    % width = 0.1:图例的宽度是图框宽度的 10%。
    % height = 0.2:图例的高度是图框高度的 20%。

    % 在指定位置添加文本
    % 获取坐标范围
    x_lim = xlim;
    y_lim = ylim;
    [x_frac, y_frac] = deal(plot_info.text_pos(1),plot_info.text_pos(2));

    x_pos = x_lim(1) + x_frac * (x_lim(2) - x_lim(1)); % 1/24 为时间跨度的1小时, 示例值,你可以根据需要调整
    y_pos = y_lim(1) + y_frac * (y_lim(2) - y_lim(1));  % 0.1 为示例值,你可以根据需要调整
    text(x_pos,y_pos,plot_info.sitesID,...
                 'FontSize',8,'Fontname','Times New Roman');
      
    %% 绘制辅助信息及图像保存
    % 添加坐标轴标题
    % 构建带有单位的标题字符串
%     xLabelStr = sprintf('%s ', plot_info.xLabel);
    yLabelStr = sprintf('%s (%s)', plot_info.yLabel, plot_info.units);
    % 设置坐标轴标签
%     xlabel(xLabelStr, 'FontName', 'Times New Roman', 'FontSize', 8);
    ylabel(yLabelStr, 'FontName', 'Times New Roman', 'FontSize', 8);
%     title(plot_info.imageName, 'FontName', 'Times New Roman', 'FontSize', 8);

    % 美化图形
%     grid on;
    box on; % 坐标图框封闭
    hold off;
    
    % 检查表格中是否存在 'save' 列,并且其值为 0
    if ismember('save', plot_info.Properties.VariableNames) && plot_info.save == 0
        % 不保存图像
        disp('图像未保存')
    else
        imageName = [plot_info.imageName, '.tiff']; % 默认使用png格式
        fullPath = fullfile(plot_info.filePath, imageName);
        % 设置要保存的分辨率
        resolution = 300; % 例如,300 DPI   
        % 保存为指定分辨率的 JPEG 图像
        print(gcf, fullPath, '-dtiff', ['-r' num2str(resolution)]);
    end

%     关闭图形窗口
    close(gcf);
%% 函数测试截止
end

  1. 函数调用:
    以下为多个变量的批量出图
    % 提前设置好每个变量的参数,这里需要测试
    range_all={[0,800],[-100,300],[-100,300],[0,800],[0,800],[0,1200],[0,300]};%轴域范围
    text_pos={[0.45,0.08], [0.45,0.08],[0.45,0.08], [0.45,0.08],...
       [0.45,0.08], [0.45,0.08],[0.45,0.08]};%text的定位
      
    ax_d=[200,100,100,200,200,300,100];%轴间距信息
      
    plot_vars= fieldnames(flux_vars);
    check_flux.(plot_vars{1})= flxdat.AAAA; 
    check_flux.(plot_vars{2})= flxdat.BBBB; 
    check_flux.(plot_vars{3})= flxdat.CCCC; 
    check_flux.(plot_vars{4})= flxdat.DDDD; 
    check_flux.(plot_vars{5})= flxdat.EEEE; 
    check_flux.(plot_vars{6})= flxdat.FFFF; 
    check_flux.(plot_vars{7})= flxdat.GGGG; 

    for j=1:length(plot_vars)
        x_obs=check_flux.(plot_vars{j});
        y_sim=flux_vars.(plot_vars{j});
        [check_vars,valid_vars]= calculate_metrics(plot_vars{j},x_obs,y_sim);%与观测数据对比

        % 绘制散点图
        plot_info=table;
        plot_info.size=[16,6];
        plot_info.range=range_all{j};%先粗出图设置为[],测试好再确定统一范围
        plot_info.ax_d=ax_d(j);%纵轴间距
        plot_info.ax_pos=[1.5,1];%定位粉框轴域
        plot_info.leg_pos=[0.5, 0.03, 0.05, 0.05];%图例位置及大小 
        plot_info.text_pos=[0.03,0.9];%[x_frac, y_frac]
        
%         plot_info.xLabel='date';
        plot_info.leg1=strcat(plot_vars{j},'_o_b_s');
        plot_info.leg2=strcat(plot_vars{j},'_m_o_d');
        plot_info.units='W/m^2';
        plot_info.sitesID=sites(i);
        plot_info.filePath=fullfile(wdir,'\data\data_check\4_AA_check\');
        plot_info.imageName=strcat(sites{i},'_scatter_',plot_vars{j});
    %     plot_info.save=0;%0代表不保存,1代表保存,如果不设置这个,默认保存
    
        plot_scatter_line(flxdat.datetime_start, x_obs,y_sim,plot_info);
        end

  1. 定位逻辑:轴域、图例、文字定位
    1)轴域定位
plot_info.ax_pos=[1.5,1];%定位粉框轴域

这句调用语句用于调整粉框轴域,可以发现相对散点图,对应plot_scatter_line.m相应位置增加了一句axes域的定位,主要是为了随意放置图例,详见我的帖子matlab绘图(1):两句代码+1张图——matlab绘图定位问题https://blog.csdn.net/CatfishYalu/article/details/140516227?csdn_share_tail=%7B%22type%22%3A%22blog%22%2C%22rType%22%3A%22article%22%2C%22rId%22%3A%22140516227%22%2C%22source%22%3A%22CatfishYalu%22%7D

2)图例定位

plot_info.leg_pos=[0.5, 0.03, 0.05, 0.05];%图例位置及大小 

具体设置详见plot_scatter_line.m函数相应位置

3)文字定位

plot_info.text_pos=[0.03,0.9];%[x_frac, y_frac]

这个的定位逻辑和plot_scatter.m是一样的,详见我的帖子matlab绘图(2):散点密度图
https://blog.csdn.net/CatfishYalu/article/details/140518511?csdn_share_tail=%7B%22type%22%3A%22blog%22%2C%22rType%22%3A%22article%22%2C%22rId%22%3A%22140518511%22%2C%22source%22%3A%22CatfishYalu%22%7D

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值