matlab中两个x轴,【MATLAB】 绘制 双 X 轴曲线

这篇博客介绍了如何在MATLAB中创建具有双X轴的曲线图,提供了`plotxx`函数的使用方法,包括参数解释和示例代码。通过这个函数,可以方便地在同一图表上展示两个独立的X轴数据系列。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

该函数调用格式为:

[ax,hl1,hl2] =

plotxx(x1,y1,x2,y2,xlabels,ylabels);

参数说明:

ax是坐标轴的句柄。h1 和 h2

是两条曲线的句柄。x1,y1,x2,y2

是绘图数据。Xlabels,Ylabels是X轴和Y州标注内容对应的细胞结构。

例:

D =

linspace(-100,0,50);%Y轴数据

S =

linspace(34,32,50);%上侧X轴数据

T =

10*exp(D/40);%下侧X轴数据

xlabels{1} = 'Temperature

(C)';%下侧X轴内容标注

xlabels{2} =

'Salinity';%上侧X轴内容标注

ylabels{1} =

'Depth(m)';%左侧Y轴内容标注

ylabels{2} =

'Depth(m)';%右侧Y轴内容标注

[ax,hlT,hlS] =

plotxx(T,D,S,D,xlabels,ylabels);%绘制双X轴曲线

a4c26d1e5885305701be709a3d33442f.png

附件:plotxx.m

function [ax,hl1,hl2] =

plotxx(x1,y1,x2,y2,xlabels,ylabels);

%PLOTXX - Create graphs with x axes

on both top and bottom

%

%Similar to PLOTYY, but

...

%the independent variable is on the

y-axis,

%and both dependent variables are on

the x-axis.

%

%Syntax: [ax,hl1,hl2] =

plotxx(x1,y1,x2,y2,xlabels,ylabels);

%

%Inputs: X1,Y1 are the data for the

first line (black)

% X2,Y2 are the data for the second line (red)

% XLABELS is a cell array containing the two x-labels

% YLABELS is a cell array containing the two y-labels

%

%The optional output handle graphics

objects AX,HL1,HL2

%allow the user to easily change the

properties of the plot.

%

%Example: Plot temperature T and

salinity S

% as a function of depth D in the ocean

%

%D =

linspace(-100,0,50);

%S =

linspace(34,32,50);

%T = 10*exp(D/40);

%xlabels{1} = 'Temperature

(C)';

%xlabels{2} =

'Salinity';

%ylabels{1} =

'Depth(m)';

%ylabels{2} =

'Depth(m)';

%[ax,hlT,hlS] =

plotxx(T,D,S,D,xlabels,ylabels);

%The code is inspired from page

10-26 (Multiaxis axes)

%of the manual USING MATLAB

GRAPHICS, version 5.

%

%Tested with Matlab 5.3.1 and above

on PCWIN

%Author: Denis Gilbert, Ph.D.,

physical oceanography

%Maurice Lamontagne Institute, Dept.

of Fisheries and Oceans Canada

%email:Web:http://www.qc.dfo-mpo.gc.ca/iml/

%November 1997; Last revision:

01-Nov-2001

if nargin <

4

error('Not enough input arguments')

elseif nargin==4

%Use empty strings for the xlabels

xlabels{1}=' '; xlabels{2}=' '; ylabels{1}=' '; ylabels{2}='

';

elseif nargin==5

%Use empty strings for the ylabel

ylabels{1}=' '; ylabels{2}=' ';

elseif nargin >

6

error('Too many input arguments')

end

if length(ylabels) ==

1

ylabels{2} = ' ';

end

if

~iscellstr(xlabels)

error('Input xlabels must be a cell array')

elseif

~iscellstr(ylabels)

error('Input ylabels must be a cell array')

end

hl1=line(x1,y1,'Color','k');

ax(1)=gca;

set(ax(1),'Position',[0.12 0.12 0.75

0.70])

set(ax(1),'XColor','k','YColor','k');

ax(2)=axes('Position',get(ax(1),'Position'),...

'XAxisLocation','top',...

'YAxisLocation','right',...

'Color','none',...

'XColor','r','YColor','k');

set(ax,'box','off')

hl2=line(x2,y2,'Color','r','Parent',ax(2));

%label the two

x-axes

set(get(ax(1),'xlabel'),'string',xlabels{1})

set(get(ax(2),'xlabel'),'string',xlabels{2})

set(get(ax(1),'ylabel'),'string',ylabels{1})

set(get(ax(2),'ylabel'),'string',ylabels{2})

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

另一种要求:只画一条曲线,但要求标注上下两个x轴的坐标

或者:

x1 =

[0:.1:40];

y1 =

4.*cos(x1)./(x1+2);

x2 =

[1:.2:20];

y2 =

x2.^2./x2.^3;

hl1 =

plot(x1,y1,'Color','r');

ax1 =

gca;

set(ax1,'XColor','r','YColor','r')

ax2 =

axes('Position',get(ax1,'Position'),...

'XAxisLocation','top',...

'YAxisLocation','right',...

'Color','none',...

'XColor','k','YColor','k');

hl2 =

line(x2,y2,'Color','k','Parent',ax2);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值