matlab中时间横坐标、双Y轴多颜色散点图及图例问题

认真读完下面这段代码你会解决时间横坐标、双Y轴范围及图例问题
a=0;
b=0;
t=0:0:0
P2=load(‘D:\P2.txt’); %导入名字P2的txt文件,文件内是行列数据
P2(:,6)=datenum(P2(:,3:5))
%将P2中第3—5列(分别为年月日)转换为matlab记录时间的形式,存到第6列中
for n=1:31 %我的数据有31行
a=a+1;%记录n
if P2(n,3)=0 %如果P2中第n行的第3列数据为0
[ax,h1,h2]=plotyy(P2(n,6),P2(n,1),P2(n,6),P2(n,2),‘scatter’)
%等号左边为plotyy的句柄ax=[h1,h2],等式右边的x轴为P2(n,6),即时间,双y轴分别为P2(n,1)和P2(n,2)
set(h1,‘marker’,‘o’);%有条件地(P2(n,3)=0)将h1画为圆圈
set(h2,‘marker’,’^’);%有条件地(P2(n,3)=0)将h2画为三角
set(h1,‘markeredgecolor’,‘r’);%均为红色
set(h2,‘markeredgecolor’,‘r’);
set(h1,‘markerfacecolor’,‘r’);
set(h2,‘markerfacecolor’,‘r’);
set(ax(2),‘ytick’,t);%将ax清零,作用是此时清除掉右边Y轴的刻度,为什么要取消呢?因为我们是每行数据画一次,要画31次,这样第二个Y轴的刻度叠加在一起很粗很不好看
if a==8
%在第八次循环时提前画出下两个散点(为了能有legend加不同颜色的图例)
这是因为在我的数据中,第8和第9个数据恰好分别应该画蓝和红点
[ax,h3,h4]=plotyy(P2(n+1,7),P2(n+1,1),P2(n+1,7),P2(n+1,2),‘scatter’)
set(h3,‘marker’,‘o’);
set(h4,‘marker’,’^’);
set(h3,‘markeredgecolor’,‘b’);%画为蓝色
set(h4,‘markeredgecolor’,‘b’);
set(h3,‘markerfacecolor’,‘b’);
set(h4,‘markerfacecolor’,‘b’);
legend([h1 h3 h2 h4],‘error data1’,‘correct data1’,‘error data2’,‘correct data2’);%此时画出双Y轴四个点的图例(一个蓝圆圈,一个红圆圈,一个蓝三角,一个红三角)legend可以用句柄做定位图例
hold on
end
hold on
else
if a~=9
[ax,h1,h2]=plotyy(P2(n,7),P2(n,1),P2(n,7),P2(n,2),‘scatter’)
set(h1,‘marker’,‘o’);
set(h2,‘marker’,’^’);
set(h1,‘markeredgecolor’,‘b’);
set(h2,‘markeredgecolor’,‘b’);
set(h1,‘markerfacecolor’,‘b’);
set(h2,‘markerfacecolor’,‘b’);
end
hold on
end
axis([737791 737821 800 1500]);%设置时间横坐标和第一个Y轴的范围
set(gca,‘XTick’,[737791:3:737821]);%设置时间横坐标的间隔
dateaxis(‘x’,6);%让横坐标显示成日期的形式
set(ax(1),‘ylim’,[800,1500],‘ytick’,[800:10:1500]);%设置第一个Y轴的范围和步长
set(ax(2),‘ylim’,[-100,100],‘ytick’,[-100:10:100]);%设置第二个Y轴的范围和步长
set(ax(2),‘ytick’,t);%取消第二个Y轴的刻度
end
set(ax(2),‘ylim’,[-100,100],‘ytick’,[-100:10:100]);%画完所有的行数据后,在最后一次画出Y的刻度和步长
ylabel(ax(1),‘Y1’);
ylabel(ax(2),‘Y2’);
xlabel(‘Date’);

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值