Labfans是一个针对大学生、工程师和科研工作者的技术社区。 论坛首页 | 联系我们(Contact Us)
MATLAB爱好者论坛-LabFans.com
返回   MATLAB爱好者论坛-LabFans.com > 工程数学软件 > MATLAB论坛
MATLAB论坛 一切MATLAB相关问题在此讨论。
回复
 
主题工具 显示模式
旧 2019-07-14, 10:43   #1
ascendgo
初级会员
 
注册日期: 2019-07-14
帖子: 1
声望力: 0
ascendgo 正向着好的方向发展
默认 matlab在画多y轴时无法正确绘图

我尝试画3个y轴,有6组数据,3组使用line画,3组使用scatter画,发现在第2组使用scatter画时,会覆盖之前的图,并且坐标轴发生变动,是为什么?
下面的程序是我在网上找的并更改后的
clc; clear;
x1 = xlsread('case.xlsx', 'case1','b2:b35');
y1 = xlsread('case.xlsx','case1','c2:c35');

x2 = xlsread('case.xlsx', 'case1','b2:b35');
y2 = xlsread('case.xlsx', 'case1','f2:f35');

x3 = xlsread('case.xlsx', 'case1','b2:b35');
y3 = xlsread('case.xlsx', 'case1','i2:i35');

x4 = xlsread('case.xlsx', 'case1','l2:l35');
y4 = xlsread('case.xlsx','case1','m2:m35');

x5 = xlsread('case.xlsx', 'case1','l2:l35');
y5 = xlsread('case.xlsx', 'case1','p235');

x6 = xlsread('case.xlsx', 'case1','l2:l35');
y6 = xlsread('case.xlsx', 'case1','s2:s35');

% plot(x1,y1,x2,y2,x3,y3,x4,y4,x5,y5,x6,y6)
ha1 = axes('ycolor','b');
hold on;
h11 = line(x4,y4,'color','b','parent',ha1);
h12 = scatter(x1,y1,'bo','parent',ha1);
hold on;
xlim1 = get(ha1,'xlim'); % 获取第一个axes的x轴的范围,这个值很关键
%
% 建立第二个轴,要与第一个轴重合,并且透明,轴的范围相同
pos1 = get(ha1,'position');
ha2 = axes('position',pos1,'color','none','ycolor','r','yaxislocation','right','xlim',xlim1);

% 画线
h21 = line(x5,y5,'color','r','parent',ha2);
h22 = scatter(x2,y2,'rs','parent',ha2);
hold on;
%
% 建立第三个轴,左侧与前两个轴对齐,右侧要长出一部分,因此该轴的范围要比前轴的范围大,根据范围与轴的长度成正比来计算
pos1(1)=pos1(1);
pos1(3) = pos1(3)*.86;
set([ha1;ha2],'position',pos1);
pos3 = pos1;
pos3(3) = pos3(3)+.12;
xlim3 = xlim1;
xlim3(2) = xlim3(1)+(xlim1(2)-xlim1(1))/pos1(3)*pos3(3);
ha3 = axes('position',pos3, 'color','none','ycolor','g','xlim',xlim3, ...
'xtick',[],'yaxislocation','right','yminortick','on');
% 截去多余数据点,并画线
% [x3, ind] = sort(x3, 2, 'ascend');
% y3 = y3(ind);
% ind2 = (x3<=xlim1(2));
% y3 = y3(ind2);
% x3 = x3(ind2);
h31 = line(x6, y6,'color','g','parent',ha3);
% h32 = scatter(x3,y3,'g^','parent',ha3);
% 隐藏第三个横轴伸出来的部分
ylim3 = get(ha3, 'ylim');
line([xlim1(2),xlim3(2)],[ylim3(1),ylim3(1)],'parent',ha3,'color','none');
% % 加ylabel
% hylab = get([ha1;ha2;ha3],'ylabel');
% hxlab = get(ha1,'xlabel');
% set(hylab{1},'string','bar');
% set(hylab{2},'string','log(x)');
% set(hylab{3},'string','tan(x)');
% % 加xlabel
% set(hxlab,'string', 'x');
ascendgo 当前离线   回复时引用此帖
回复


发帖规则
不可以发表新主题
不可以发表回复
不可以上传附件
不可以编辑自己的帖子

启用 BB 代码
论坛启用 表情符号
论坛启用 [IMG] 代码
论坛禁用 HTML 代码



所有时间均为北京时间。现在的时间是 22:15


Powered by vBulletin
版权所有 ©2000 - 2025,Jelsoft Enterprises Ltd.