matlab画左侧_右侧三纵坐标轴和其他样式的

文摘   2024-11-08 02:47   德国  

matlab画左侧_右侧三纵坐标图和其他样式的


先展示结果图。最后文末放代码进行复制黏贴。

代码中有注释介绍,可以帮助理解!


左侧三纵坐标轴:



右侧三纵坐标轴:

三纵坐标轴其他样式:

一、

二、

三、

四、

关键节点提示:


对于添加第三个坐标轴,数字覆盖问题两个解决方案

1:改变ylim,字体上移,不在x坐标轴上;

2:最后,text这个字体覆盖掉;




对于添加第三个y坐标轴:

pos=get(gca,'position');% 得到位置大小

offset = pos(3)/5.5;% 调整控制左侧坐标轴距离主框的距离%

pos3 = [pos(1) - offset pos(2) pos(3)+offset pos(4)];% 因此,第一个位置需要往左移动。

设置第三个画图位置:

ax(3) = axes('Position',pos3)

设置第三个图的x范围控制在前两个的范围上显示,以达到共用x刻度:

xlim([limx3(1) limx3(2)])


代码:

左侧三纵坐标:

clc,clear;load('data.mat')
x = 1:18;% 数据长度的x数组

close allfigureset(gcf,'position',[50 50 950 750],'color','w')yyaxis left% 左侧x1=plot(x,sst1,"LineWidth",2,'color','k');set(gca,'xtick',[1:4:18],'xticklabel',1997:4:2020)%x轴刻度set(gca,'fontsize',12,'fontweight','bold','linewidth',2,'ycolor','k')ylim([-3.1 3])% 这个左侧本来是-3 to 3 这里给-3.1 是因为添加第三坐标轴的时候,会覆盖。你可以改下试试;box offyyaxis right % 右侧y1=plot(x,wind1,'-','color','r','linewidth',1.5);hold on;set(gca,'XColor','k','YColor','r');% y轴的颜色set(gca,'fontsize',12,'fontweight','bold','linewidth',2)% 字体box off% 以下添加第三个坐标轴,在左侧添加poss = [0.3 0.1 0.65 0.8];% 给一个大小的框,在此基础上左侧添加y坐标轴set(gca,'position',poss)pos=get(gca,'position');offset = pos(3)/5.5;% 控制左侧坐标轴距离主框的距离pos3 = [pos(1) - offset pos(2) pos(3)+offset pos(4)];% 因此,第一个位置需要往左移动。scale4 = pos3(3)/pos(3);limx1 = get(gca,'xlim');limx3 = [limx1(2)-scale4*(limx1(2)-limx1(1)) limx1(2)];% 相应的x轴的范围需要调整,以让线条在同一个x范围内显示,使用同一个x坐标轴。ax(3) = axes('Position',pos3,'box','off',... 'Color','none','XColor','k','YColor','b',... 'xtick',[],'xlim',limx3,'yaxislocation','left');y11=plot(x,heatflux1,'-','color','b','linewidth',1.5);set(gca,'fontsize',12,'fontweight','bold','linewidth',2,'color','none','YColor','b','xtick',[])xlim([limx3(1) limx3(2)])box offylimy = get(gca,'ylim');line([limx3(1)+0.3 limx1(1)],[ylimy(1) ylimy(1)],'color','w','linewidth',3)
export_fig('left_three_axis.jpg','-r600')


右侧三纵坐标:


% one_pictureclear;clc;close all;load('data1.mat')%%  xtick according sample sizexti =[0.107441159848809;0.128929391818571;0.150417623788332;0.171905855758094;0.193394087727856;0.2148823196100618;0.236370551667379;0.257858783637141;0.279347015606903;0.300835247576665;0.322323479546426;0.343811711516188;0.365299943485950;0.386788175455712;0.408276407425473;0.4210064639395235;0.4512528713649100;0.472741103334759;0.494229335304520;0.515717567274282;0.537205799244044;0.558694031213806;0.580182263183568;0.601670495153329;0.623158727123091;0.644646959092853;0.666135191062615;0.687623423032376;0.709111655002138;0.7305998861001900;0.752088118941662;0.773576350911423;0.795064582881185;0.816552814850947;0.838041046820709;0.859529278790470;0.881017510760232;0.902505742729994;0.92399310046910056;0.945482206669517];xti = xti*100;xti = roundn(xti,-1);%% 画图mycolors1 =[0,0,255]./255;mycolors2 =[255,0,255]./255;%  plotclose allfigureset(gcf,'Position',[50 50 1200 850],'Color','w')% 建第一个axesha(1) = axes('ycolor','b','yminortick','off','xminortick','off');hold on; % 保持以上属性,因为下面的bar有自动调整axes属性的作用h2=plot(xti,squeeze(mean(depth_IT_100,2)),'color','b','linewidth',2,'parent',ha(1));hold on set(gca,'xtick',[0:10:100],'fontsize',14,'fontname','time news roman','LineWidth',2,'fontweight','bold')xtickformat('%.0f \%')xlim([0 100])box offxlabel('Percentage','fontsize',14,'fontname','time news roman','fontweight','bold')ylabel('heatflux','fontsize',16,'fontname','time news roman','fontweight','bold')xlim1 = get(ha(1),'xlim'); % 获取第一个axes的x轴的范围,这个值很关键% 建立第二个轴,要与第一个轴重合,并且透明,轴的范围相同pos1=get(ha(1),'position');ha(2) = axes('position',pos1,'color','none','ycolor','r','yaxislocation','right','xlim',xlim1,  'xtick',  []);plot(xti,squeeze(mean(depth_ST_100,2)),'color','r','linewidth',2,'parent',ha(2))hold on set(gca,'xtick',[],'XTickLabel',[],'color','none','fontsize',14,'fontname','time news roman','LineWidth',2,'fontweight','bold','ycolor','r','yaxislocation','right','xlim',xlim1)box offylabel('wind','fontsize',16,'fontname','time news roman','fontweight','bold')% 建立第三个轴,左侧与前两个轴对齐,右侧要长出一部分,因此该轴的范围要比前轴的范围大,根据范围与轴的长度成正 % 比来计算pos1(1)=pos1(1)-0.02;pos1(3) = pos1(3)*.86;set([ha(1);ha(2)],'position',pos1);pos3 = pos1;pos3(3) = pos3(3)+.12;xlim3 = xlim1;xlim3(2) = xlim3(1)+(xlim1(2)-xlim1(1))/pos1(3)*pos3(3);ha(3) = axes('position',pos3, 'color','none','ycolor',[0,0.5,0],'xlim',xlim3, ...    'xtick',[],'yaxislocation','right','yminortick','on');% 截去多余数据点,并画线x3 = xti;fre_mean = nanmean(fre,2);y3 = mapminmax(fre_mean',0.1,0.9);[x3, ind] = sort(x3, 2, 'ascend');y3 = y3(ind);ind2 =  (x3<=xlim1(2));y3 = y3(ind2);x3 = x3(ind2);hold on% xlim([0 100])ylim([0 1])ylabel('sst','fontsize',12,'fontname','time news roman','fontweight','bold')plot(xti,mapminmax(fre_mean',0.1,0.9),'color','k','linewidth',2,'parent',ha(3))set(gca,'color','none','ycolor','k','xlim',xlim3, ...    'xtick',[],'yaxislocation','right','yminortick','off','fontsize',14,'fontname','time news roman','LineWidth',2,'fontweight','bold')box off% 隐藏第三个横轴伸出来的部分ylim3 = get(ha(3), 'ylim');line([xlim1(2)+0.15,xlim3(2)-1.5],[ylim3(1),ylim3(1)],'parent',ha(3),'color','w','LineWidth',3);hold online([xlim1(1) xlim1(1)+1.5],[ylim3(1),ylim3(1)],'parent',ha(3),'color','b','LineWidth',2.2);hold online([xlim1(2)-1.5 xlim1(2)+0.15],[ylim3(1),ylim3(1)],'parent',ha(3),'color','r','LineWidth',2.2);text(xlim1(2)+0.8,ylim3(1),'-7.485','fontsize',14,'fontname','time news roman','fontweight','bold','color','r')export_fig('右侧.jpg','-r700')


其他数据代码:更多上传到群。

可分享获得




海洋与大气科学
海洋与大气科学数据分析,数据可视化分享,可教学。
 最新文章