matlab画图相关设置

目录

  • 多子图
    • 多个pcolor或contourf只显示一个colorbar
    • 用函数实现子图位置
  • 我常用的设置
    • 填色图
    • 线图散点图

多子图

多个pcolor或contourf只显示一个colorbar

用axes定义坐标轴位置来实现,如果只对某一个subplot显示colorbar,会有图像大小不一致的问题。

demo:

top_margin = 0.04;      
btm_margin = 0.06;      
left_margin = 0.06;     
right_margin = 0.08;    
fig_margin = 0.05;      
row = 3;                 
col = 2;                 
clim = [-1 16];         
fig_height = (1- top_margin - btm_margin - (row-1) * fig_margin) / row;
fig_width = (1 - left_margin - right_margin - (col-1) * fig_margin) / col;
figure
count=0;
for i = 1 : row
    for j = 1 : col
        position = [left_margin + (j-1)*(fig_margin+fig_width),...
        1- (top_margin + i * fig_height + (i-1) * fig_margin), fig_width, fig_height];
       axes('position', position);
       count=count+1;
       pcolor(magic(count+6));
       shading flat
       title(['Magic',num2str(count+6)]);
       set(gca,'xticklabel',[]);
       set(gca,'yticklabel',[]);
    end
end
axes('position', [1-right_margin-left_margin-(col-1)*fig_margin, btm_margin, 0.2, 1-(top_margin+btm_margin)]);
axis off;
colorbar();caxis(clim);

宽度、图像间距这些参数根据需要微调,示例是生成多个幻方矩阵,下图是输出结果,奇数阶是对称的。
matlab画图相关设置_第1张图片

用函数实现子图位置

subplot的子图之间间距略宽
也可以用一个函数把上面生成多个axes位置的语句包装起来,函数如下:

function aaexs = tight_subplot(row, col, gap, marg_h, marg_w)
if nargin<3; gap = .02; end
if nargin<4 || isempty(marg_h); marg_h = .05; end
if nargin<5; marg_w = .05; end

if numel(gap)==1
    gap = [gap gap];
end
if numel(marg_w)==1
    marg_w = [marg_w marg_w];
end
if numel(marg_h)==1
    marg_h = [marg_h marg_h];
end

axh = (1-sum(marg_h)-(row-1)*gap(1))/row; 
axw = (1-sum(marg_w)-(col-1)*gap(2))/col;

py = 1-marg_h(2)-axh; 

aaexs = zeros(row*col,1);
ii = 0;
for ih = 1:row
    px = marg_w(1);

    for ix = 1:col
        ii = ii+1;
        aaexs(ii) = axes('Units','normalized', 'Position',[px py axw axh],  'XTickLabel','',  'YTickLabel','');
        px = px+axw+gap(2);
    end
    py = py-axh-gap(1);
end

我常用的设置

填色图

结合worldmap(需要安装map工具箱),demo:

 figure;
m_proj('Equidistant','lat',[-90 90],'lon',[0 360]);    
m_contourf(lon,lat,k2',50,'linestyle','none'); 
m_grid('linestyle','none','tickdir','out','linewidth',2);
m_coast('patch',[0.5,0.5,0.5],'edgecolor','k','facecolor',[0.8,0.8,0.8]); 
hold on 
m_plot(xl,yl,'.','color',[0.5,0.5,0.5],'markersize',5)
title('The SST Trend Between 2000-2015 [Sen] [℃/Decade]','FontSize',18);
colormap(mycolor)
colorbar
set(gca,'Clim',[-1 1]);
% print('The SST Trend [2000-2015]sen','-depsc2','-r300','-loose')

输出效果
matlab画图相关设置_第2张图片

线图散点图

scatter叠加拟合的plot,demo:

aa=lsqcurvefit(@myrelation,[2 1],log(x(~isnan(x))),log(y(~isnan(y))));
yy=exp(zr_relation(aa,log(x)));
loglog(x,yy,'linewidth',1.5);
hold on
scatter(x,y,20,'filled')
text(0.1,50000,'c) test','fontsize',12);
set(gca,'YTick',[-1 0 1 2 3 4],'YTickLabel',{'0.1','1','2','4','8','16'});

效果类似下图
matlab画图相关设置_第3张图片

你可能感兴趣的:(matlab)