简体   繁体   English

Matlab图中有3个x轴?

[英]3 x-axis in matlab plot?

I need to plot a figure with 3 x-axes. 我需要绘制一个带有3个X轴的图形。 Each axis is linked to the other by a mathematical formula. 每个轴通过数学公式相互链接。 I want to do this because the x value can be seen as wavelength [nm], velocity [m/s] or energy [eV] and I want the reader to not have to convert it themselves on each graph. 我之所以想这样做,是因为x值可以看成是波长[nm],速度[m / s]或能量[eV],并且我希望读者不必在每个图形上都进行转换。

I searched online and only found something for 2 x-axes , but no more. 我在网上搜索,只找到2个x轴的东西 ,但没有更多。

Edit : I am using version R2011a. 编辑 :我正在使用版本R2011a。

So it should look like this, which I (obviously) didn't create in MATLAB: 所以它看起来应该是这样的,我(显然)不是在MATLAB中创建的:

我要绘制的内容(这在Matlab上还没有完成...)

Thanks in advance! 提前致谢!

As shown in this answer , you can create a new axes object with near-zero height, so that it is essentially just the x-axis. 该答案所示,您可以创建一个高度接近零的新axes对象,因此它实际上只是x轴。 Be aware that all actual plots must be done on the first axes as this is the area you can see! 请注意,所有实际绘图都必须在第一个轴上完成,因为这是您可以看到的区域!

Demo code: 演示代码:

% Create some plotting data and plot
x = 0:0.1:2*pi;   y = sin(x);
% Plot, can specify line attributes (like LineWidth) either 
% - inline: plot(x,y,'linewidth',2)
% - after: p1 = plot(x,y); p1.LineWidth = 2;
plot(x,y);
% Get current axes object (just plotted on) and its position
ax1 = gca;
axPos = ax1.Position;
% Change the position of ax1 to make room for extra axes
% format is [left bottom width height], so moving up and making shorter here...
ax1.Position = axPos + [0 0.3 0 -0.3];
% Exactly the same as for plots (above), axes LineWidth can be changed inline or after
ax1.LineWidth = 2;
% Add two more axes objects, with small multiplier for height, and offset for bottom
ax2 = axes('position', (axPos .* [1 1 1 1e-3]) + [0 0.15 0 0], 'color', 'none', 'linewidth', 2);
ax3 = axes('position', (axPos .* [1 1 1 1e-3]) + [0 0.00 0 0], 'color', 'none', 'linewidth', 2);
% You can change the limits of the new axes using XLim
ax2.XLim = [0 10];
ax3.XLim = [100 157];
% You can label the axes using XLabel.String
ax1.XLabel.String = 'Lambda [nm]';
ax2.XLabel.String = 'Velocity [m/s]';
ax3.XLabel.String = 'Energy [eV]';

Output: 输出:

多个x轴MATLAB


Edit : 编辑
Before the 2014b graphics changes you will need to make a couple of tweaks for getting and setting axes properties. 更改2014b图形之前,您需要进行一些调整以获取和设置轴属性。 The equivalent code would more heavily use the set command, and look something like this: 等效代码将更大量地使用set命令,并且看起来像这样:

x = 0:0.1:2*pi;   y = sin(x);
plot(x,y);
ax1 = findobj(gca, 'type', 'axes')
axPos = get(ax1, 'Position');
set(ax1, 'Position', axPos + [0 0.3 0 -0.3]);
set(ax1, 'LineWidth', 2);
ax2 = axes('position', (axPos .* [1 1 1 1e-3]) + [0 0.15 0 0], 'color', 'none', 'linewidth', 2);
ax3 = axes('position', (axPos .* [1 1 1 1e-3]) + [0 0.00 0 0], 'color', 'none', 'linewidth', 2);
set(ax2, 'xlim', [0 10]);
set(ax3, 'xlim', [100 157]);
axes(ax1); xlabel('Lambda [nm]');
axes(ax2); xlabel('Velocity [m/s]');
axes(ax3); xlabel('Energy [eV]');

Here's an example of how you can do this: 以下是如何执行此操作的示例:

msx = [1 50 60 90];
msy = [0 1 3 8];

lx = 90/4*[1 2 3 4]; % Scale the data with respect to the data that will use the "primary" X-axis
ly = [0 2 8 10];

evx = 90/19*[1 7 10 19]; % Scale the data with respect to the data that will use the "primary" X-axis
evy = [0 8 16 20];

figure
a=axes('units','normalized','position',[.1 .35 .7 .6],'xlim',[0 100],'xtick',0:10:100);
plot(lx, ly)
hold on
plot(msx, msy)
hold on
plot(evx, evy)
xlabel(a,'velocity m/s')
b=axes('units','normalized','position',[.1 .21 .7 0.000001],'xlim',[0 4],'color','none', 'xtick',0:1:10);
xlabel(b,'lambda nm');
c=axes('units','normalized','position',[.1 .10 .7 0.000001],'xlim',[0 19],'color','none', 'xtick',0:1:19);
xlabel(c,'energy eV');

For the position: specified as a four-element vector of the form [left bottom width height]. 对于位置:指定为[左底部宽度高度]形式的四元素向量。 The default value of [0 0 1 1] includes the whole interior of the container. 默认值[0 0 1 1]包括容器的整个内部。 (see https://de.mathworks.com/help/matlab/ref/axes-properties.html ) (请参阅https://de.mathworks.com/help/matlab/ref/axes-properties.html

Output figure: 输出图: 在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM