matlab绘图实例-绘制双纵轴曲线图

这篇博客详细介绍了如何使用MATLAB进行双轴图表的绘制,包括设置纵坐标范围、线型、坐标轴距离以及刻度显示。同时,展示了如何处理左纵轴刻度线对右纵轴的影响,并通过添加文本框实现纵轴刻度的科学计数法表示。
摘要由CSDN通过智能技术生成
clc
clear

x = linspace(0,1,10);
y1 = x;
y2 = -1e5*(x-1);

figure;
[Axess, Line1, Line2] = plotyy(x,y1, x,y2);

%%% 设置纵坐标范围
set(Axess(2), 'YLim', [0,1e5])

%%% 设置线型
set(Line1,'LineStyle','-','color','b', 'LineWidth',1.5);
set(Line2,'LineStyle','--','color','r','LineWidth',1.5);

%%% 计算当前横纵坐标轴长度
Line1_xlim=xlim(Axess(1));
Line1_ylim=ylim(Axess(1));
Line1_xlim_length=Line1_xlim(2) - Line1_xlim(1);
Line1_ylim_length=Line1_ylim(2) - Line1_ylim(1);
Line2_xlim=xlim(Axess(2));
Line2_ylim=ylim(Axess(2));
Line2_xlim_length=Line2_xlim(2) - Line2_xlim(1);
Line2_ylim_length=Line2_ylim(2) - Line2_ylim(1);

%%% 设置xlabel和ylabel
xlabel('$x$','Interpreter','latex','FontSize',20)
ylabel(Axess(1), '$y_1$','Interpreter','latex','color','b','FontSize',20,'rotation',0,'position',[-Line1_xlim_length/10     Line1_ylim_length/2]); % left y-axis
ylabel(Axess(2), '$y_2$','Interpreter','latex','color','r','FontSize',20,'rotation',0,'position',[Line2_xlim_length*11.1/10 Line2_ylim_length*3.05/5]); % right y-axis

%%% 设置坐标轴距离画板(图形窗口figure)边距(0--1)-以便完全显示ylabel
% gcf 返回当前Figure 对象的句柄值
% gca 返回当前axes 对象的句柄值
% gco 返回当前鼠标单击的句柄值,该对象可以是除root 对象外的任意图形对象,并且Matlab 会把当前图形对象的句柄值存放在Figure 的CurrentObject属性中。
set (gca,'position',[0.12,0.15,0.75,0.75] );

%%% 设置刻度线:处理左纵轴刻度线对右纵轴的影响
%%% 设置左纵轴
set(Axess(1),'XColor','k','YColor','b'); % 这里XColor控制下边横轴、刻度和刻度线的颜色;YColor控制左边纵轴、刻度和刻度线的颜色
%%% 设置右纵轴
box off
ax2 = axes('Position',get(gca,'Position'),...
           'XAxisLocation','top',...
           'YAxisLocation','right',...
           'Color','none',...
           'XColor','k','YColor','r'); % 这里XColor控制上边横轴的颜色,YColor控制右边纵轴的颜色
set(ax2,'YTick', []);
set(ax2,'XTick', []);
set(Axess(2),'XColor','k','YColor','r'); % 这里XColor无用;YColor控制右边刻度和刻度线的颜色

%%% 设置在哪些值显示刻度线并设置对应的刻度值
set(Axess(1),'ytick',[0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1],'yticklabel',{'0','0.1','0.2','0.3','0.4','0.5','0.6','0.7','0.8','0.9','1'}) 
set(Axess(2),'ytick',[0 0.25 0.5 0.75 1]*1e5,'yticklabel',{'0','0.25','0.5','0.75','1'}) 

%%% 通过添加文本框设置纵轴刻度科学计数法表示
annotation('textbox',...
    [0.868113095238096 0.921002456408551 0.0806964285714273 0.0682539682539709],...
    'Color',[1 0 0],...
    'String',{'\times10^5'},...
    'FitBoxToText','off',...
    'EdgeColor','none');

在这里插入图片描述

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值