2017-07-17 84 views
1

我需要绘制一个带有3个x轴的图形。每个轴都通过一个数学公式链接到另一个轴。我想这样做是因为x值可以看作波长[nm],速度[m/s]或能量[eV],我希望读者不必在每张图上自行转换它。3 x轴在matlab中的图?

我在网上搜索,只发现something for 2 x-axes,但没有更多。

编辑:我正在使用版本R2011a。

因此,它应该是这样的,这是我(显然)没在MATLAB创建:

What I 'd like to plot (it is not done on matlab this one...)

提前感谢!

回答

3

this answer所示,您可以创建一个接近零高度的新对象,以便它基本上只是x轴。请注意,所有实际图必须在第一轴上完成,因为这是您可以看到的区域!

演示代码:

% 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]'; 

输出:

multiple x axes matlab


编辑
之前2014b graphics changes,你需要做一些调整的获取和设置轴属性。等效代码将更多地使用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]'); 
+0

谢谢您的回答!它工作得很好:)并增加线宽它是ax1.LineWidth = ....? – John

+0

绘制线的线宽?请参阅我在代码中添加的其他评论。 – Wolfie

+0

不,轴的宽度。当它很厚时,我喜欢它!通常我用这个:'set(gca,'FontSize',13,'fontName','calibri','LineWidth',2.0,'FontWeight','bold');'现在我必须改变gca ax1 ,ax2和ax3? (我现在不能尝试,我没有对我的电脑的访问) – John

1

这里是你如何能做到这样一个例子:

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'); 

对于位置:指定为四表单元素向量[左下宽度高度]。默认值[0 0 1 1]包括容器的整个内部。 (见https://de.mathworks.com/help/matlab/ref/axes-properties.html

输出图: enter image description here