【MATLAB基础绘图第13棒】绘制三Y轴坐标图:补充坐标轴及字体设置

本文介绍了如何使用MATLAB的multiplotyyy函数绘制三轴坐标图,包括基础图形绘制、坐标轴及字体设置、颜色更改以及解决坐标轴刻度不一致的问题。案例中详细展示了代码实现过程,提供了调整坐标轴间距和创建图例的方法。
摘要由CSDN通过智能技术生成

三轴坐标图

1 函数

MATLAB绘制三轴图函数可见MATLAB帮助-multiplotyyy

2 案例

本文以以下几个例子为例,希望可以解决在利用MATLAB绘制三轴坐标图时常见的疑惑。

2.1 案例1:绘图及基础设置

根据multiplotyyy函数,基础图形绘制很简单,但坐标轴及字体设置该如何实现呢?此例将进行说明:
根据给定案例,添加坐标轴及字体设置,成图如下:
在这里插入图片描述
MATLAB相关代码如下:

clc
close all
clear
%% 导入数据
pathFigure= '.\Figures\' ;
figureUnits = 'centimeters';
figureWidth = 25; 
figureHeight = 15;

x1 = (0:0.01:1)'; 
x2 = (0:0.1:1)';
x3 = (0:0.05:1)';
y1 = x1;
y2 = x2.^2;
y3 = x3.^3;
y4 = sin(x1);
y5 = fliplr(2*x1.^2);
y6 = 7*cos(x1);
y7 = 7*log(x1+1.2);

%% 开始绘图

ylabels{1}='First y-label';
ylabels{2}='Second y-label';
ylabels{3}='Third y-label';
[ax,hlines] = multiplotyyy({x1,y1,x2,y2,x3,y3,x1,y4},{x1,y5},{x1,[y6,y7]},ylabels);
set(gcf, 'Units', figureUnits, 'Position', [0 0 figureWidth figureHeight]);
set(hlines{1}(:),'LineStyle','-','Marker','o','MarkerFaceColor','b','Markersize',3);
set(hlines{2}(:),'LineStyle','--','Marker','o','MarkerFaceColor','g','Markersize',3);
set(hlines{3}(:),'LineStyle','none','Marker','o','MarkerFaceColor','r','Markersize',3);
xlabel(ax(1),"X",'FontName','Times New Roman','FontSize',12);  
ylabel(ax(1),"Y1",'FontName','Times New Roman','FontSize',12);  
ylabel(ax(2),"Y2",'FontName','Times New Roman','FontSize',12);  
ylabel(ax(3),"Y3",'FontName','Times New Roman','FontSize',12);  
set(ax(1),'xlim',[0 1],'xtick',0:0.1:1,'xticklabel', 0:0.1:1 ,'FontSize',12,'FontName','Times New Roman');
set(ax(1),'ylim',[0 1],'ytick',0:0.1:1,'yticklabel',0:0.1:1,'FontSize',12,'FontName','Times New Roman');
set(ax(2),'ylim',[0 2],'ytick',0:0.2:2,'yticklabel',0:0.2:2,'FontSize',12,'FontName','Times New Roman');
set(ax(3),'ylim',[0 7],'ytick',0:1:7,'yticklabel',0:1:7,'FontSize',12,'FontName','Times New Roman');
hl = legend(cat(1,hlines{:}),'a','b','c','d','e','f','g','location','w');
set(hl,'Box','off','location','NorthWest','NumColumns',2,'FontSize',12,'FontName','Times New Roman');    
set(gca,'FontSize',12,'Fontname', 'Times New Roman');

str= strcat(pathFigure, "Fig.1", '.tiff');
print(gcf, '-dtiff', '-r600', str);

2.2 案例2:更改坐标轴颜色

此外,可根据需要更改坐标轴颜色,成图如下:
在这里插入图片描述
MATLAB相关代码如下:

clc
close all
clear
%% 导入数据
pathFigure= '.\Figures\' ;
load('Streamflow.mat');
ylabels{1}='干旱历时/月';
ylabels{2}='干旱烈度';
ylabels{3}='干旱强度';
x1 = 1:nYear;
mycolor = [0 0 0;
          77,133,189;
          237,119,69;
          80 80 80]/255;
  figureUnits = 'centimeters';
figureWidth = 25; 
figureHeight = 15;

% 站点1:安宁渡
% ---------------------------------------------------------------------------------
[ax,hlines] = multiplotyyy({x1,droughtCharacter{1,1}(:,3)},{x1,droughtCharacter{1,1}(:,4)},{x1,droughtCharacter{1,1}(:,5)},ylabels);
set(gcf, 'Units', figureUnits, 'Position', [0 0 figureWidth figureHeight]);
set(hlines{1}(:),'LineStyle','-','linewidth',1.5,'color',mycolor(4,:),'Marker','o','MarkerFaceColor',mycolor(4,:),'Markersize',3);
set(hlines{2}(:),'LineStyle','--','linewidth',1.5,'color',mycolor(2,:),'Marker','^','MarkerFaceColor',mycolor(2,:),'Markersize',3);
set(hlines{3}(:),'LineStyle','none','linewidth',1.5,'color',mycolor(3,:),'Marker','.','MarkerFaceColor',mycolor(3,:),'Markersize',12);
set(ax(1),'xlim',[0  nYear+1],'xtick',1:5:nYear+1,'xticklabel', yearStart:5:yearEnd ,'FontSize',12,'FontName','Times New Roman','XColor',mycolor(1,:));
set(ax(1),'ylim',[0 33],'YAxisLocation','left','ytick',0:6:33,'yticklabel',0:6:33,'FontSize',12,'FontName','Times New Roman','YColor',mycolor(1,:));
set(ax(2),'ylim',[0 36],'ytick',0:5:36,'yticklabel',0:5:36,'FontSize',12,'FontName','Times New Roman','YColor',mycolor(2,:));
set(ax(3),'ylim',[0 1.8],'ytick',0:0.3:1.8,'yticklabel',0:0.3:1.8,'FontSize',12,'FontName','Times New Roman','YColor',mycolor(3,:));
xlabel(ax(1),"年份",'FontName','宋体','FontSize',12);  
ylabel(ax(1),ylabels{1},'FontName','宋体','FontSize',12,'Fontweight','bold');  
ylabel(ax(2),ylabels{2},'FontName','宋体','FontSize',12,'Fontweight','bold');  
ylabel(ax(3),ylabels{3},'FontName','宋体','FontSize',12,'Fontweight','bold');  

str= strcat(pathFigure, "Fig. ", '.tiff');
print(gcf, '-dtiff', '-r600', str);

根据此案例可以发现,当坐标轴1和坐标轴2刻度不一致时,图1右侧刻度会与坐标轴2刻度同时出现,那么,此问题应该如何解决呢?

2.3 案例3:解决坐标轴1和2刻度不一致问题

成图如下:
在这里插入图片描述
对比案例2,修改代码如下:

[ax,hlines] = multiplotyyy({x1,droughtCharacter{1,1}(:,3)},{x1,droughtCharacter{1,1}(:,4)},{x1,droughtCharacter{1,1}(:,5)},ylabels);
set(gcf, 'Units', figureUnits, 'Position', [0 0 figureWidth figureHeight]);
set(hlines{3}(:),'LineStyle','none','linewidth',1.5,'color',mycolor(3,:),'Marker','.','MarkerFaceColor',mycolor(3,:),'Markersize',14);
set(hlines{1}(:),'LineStyle','-','linewidth',1.5,'color',mycolor(4,:));%,'Marker','o','MarkerFaceColor',mycolor(4,:),'Markersize',3);
set(hlines{2}(:),'LineStyle',':','linewidth',1.5,'color',mycolor(2,:));%,'Marker','^','MarkerFaceColor',mycolor(2,:),'Markersize',3);
set(ax(1),'xlim',[0  nYear+1],'xtick',1:5:nYear+1,'xticklabel', yearStart:5:yearEnd ,'FontSize',12,'FontName','Times New Roman','XColor',mycolor(1,:));
set(ax(1),'ylim',[0 33],'YAxisLocation','left','ytick',0:6:33,'yticklabel',0:6:33,'FontSize',12,'FontName','Times New Roman','YColor',mycolor(1,:),'LineWidth',0.5,'box', 'off');
ax1 = axes('Position',get(ax(1),'Position'),...
          'XAxisLocation','top',...
          'YAxisLocation','left',...
          'Color','none',...
          'YColor','k');
set(ax1,'XTick', [],'YTick', []);
set(ax(2),'ylim',[0 36],'ytick',0:5:36,'yticklabel',0:5:36,'FontSize',12,'FontName','Times New Roman','YColor',mycolor(2,:),'LineWidth',1);
set(ax(3),'ylim',[0 1.8],'ytick',0:0.3:1.8,'yticklabel',0:0.3:1.8,'FontSize',12,'FontName','Times New Roman','YColor',mycolor(3,:),'LineWidth',1);
xlabel(ax(1),"年份",'FontName','宋体','FontSize',14);  
ylabel(ax(1),ylabels{1},'FontName','宋体','FontSize',14,'Fontweight','bold');  
ylabel(ax(2),ylabels{2},'FontName','宋体','FontSize',14,'Fontweight','bold');  
ylabel(ax(3),ylabels{3},'FontName','宋体','FontSize',14,'Fontweight','bold');  


str= strcat(pathFigure, "Fig. 站点1:安宁渡", '.tiff');
print(gcf, '-dtiff', '-r600', str);

2.4 案例4:设置右侧俩坐标轴间距

根据案例3,发现右侧两坐标轴间距过大,通过修改原函数代码可调整其间距,成图如下所示:
在这里插入图片描述
具体修改multiplotyyy函数,代码修改参数如下:

% Set the axes position and size
pos = [0.1  0.1  0.8  0.8];
offset = pos(3)/8;
pos(3) = pos(3) - offset;
ax(1).Position = pos;
ax(2).Position = pos;

% Determine the position of the third axes
pos3 = [pos(1) pos(2) pos(3)+offset pos(4)];

% Determine the proper x-limits for the third axes
limx1 = ax(1).XLim;
limx3 = [limx1(1)   limx1(1) + 1.125*(limx1(2)-limx1(1))];

添加图例说明的代码如下:

hl = legend(cat(1,hlines{:}),"干旱历时","干旱烈度","干旱强度",'location','w');
set(hl,'Box','off','location','NorthEast','NumColumns',2,'FontSize',14,'FontName','宋体');    

参考

1.MATLAB帮助-multiplotyyy
引用格式:
Laura Proctor (2023). multiplotyyy (https://www.mathworks.com/matlabcentral/fileexchange/39595-multiplotyyy), MATLAB Central File Exchange.

  • 5
    点赞
  • 57
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
您可以使用MATLAB中的multiplotyyy函数来设置坐标轴。根据引用中的示例,可以通过修改multiplotyyy函数的代码来调整坐标轴之间的间距。具体的修改参数如下: ```matlab % 设置坐标轴的位置和大小 pos = [0.1 0.1 0.8 0.8]; offset = pos(3)/8; pos(3) = pos(3) - offset; ax(1).Position = pos; ax(2).Position = pos; % 确定第坐标轴的位置 pos3 = [pos(1) pos(2) pos(3) offset pos(4)]; % 确定第坐标轴的合适的x轴限制 limx1 = ax(1).XLim; limx3 = [limx1(1) limx1(1) 1.125*(limx1(2)-limx1(1))]; ``` 这段代码将根据您的需求调整坐标轴的位置和大小,并设置坐标轴的x轴限制。您可以根据具体的情况进行调整。 另外,您还可以使用set函数和形属性来设置坐标轴的刻度和网格线。根据引用中的示例,可以使用以下代码来设置坐标轴的刻度和开启网格线: ```matlab set(gca,'Xtick',[pi/2,pi,3.5/2*pi,2*pi],'Ytick',[-1,0.5,0,0.5,1]) grid on box off ``` 这段代码将设置x轴和y轴的刻度位置,并开启坐标轴的网格线。 如果您需要更多关于MATLAB设置坐标轴的帮助,您可以参考引用中提供的MATLAB帮助文档。 希望对您有所帮助!<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [【MATLAB基础绘图13绘制Y轴坐标补充坐标轴字体设置](https://blog.csdn.net/qq_44246618/article/details/129407893)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* [MATLAB可视化(四)如何设置形的坐标范围、坐标系、刻度和网格控制](https://blog.csdn.net/m0_73982095/article/details/130585995)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

WW、forever

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值