两个堆叠的x轴之间的垂直线

时间:2017-12-18 19:18:44

标签: matlab matlab-figure axes

我按照问题“How to insert two X axis in a Matlab plot”的解决方案来创建一个带有两个x轴的图形,一个在另一个上面。

我现在正尝试在特定x值的两个x轴之间创建一条垂直线。例如,假设我有一个像链接问题中的那个数字。如何在两个x轴之间的值x = 2 m/s处绘制垂直线?

Here is an image of what I am looking for. The red line is what I am trying to draw in MATLAB. The two x-axes both have the same scale.

1 个答案:

答案 0 :(得分:0)

这是可行的,但很棘手。我使用annotation但您需要从图形单位映射到轴单位。在这里:

% experimental data
M(:,1) = [ 0,  1,  2,  3,  4,  5];
M(:,3) = [12, 10, 15, 12, 11, 13];

% get bounds
xmaxa = max(M(:,1))*3.6;    % km/h
xmaxb = max(M(:,1));        % m/s

figure;

% axis for m/s
b=axes('Position',[.1 .1 .8 eps]);
set(b,'Units','normalized');
set(b,'Color','none');

% axis for km/h with stem-plot
a=axes('Position',[.1 .2 .8 .7]);
set(a,'Units','normalized');
stem(a,M(:,1).*3.6, M(:,3));

% set limits and labels
set(a,'xlim',[0 xmaxa]);
set(b,'xlim',[0 xmaxb]);
xlabel(a,'Speed (km/h)')
xlabel(b,'Speed (m/s)')
ylabel(a,'Samples');
title(a,'Double x-axis plot');

% this where the trick happens
pos = get(b, 'Position') ;
x_normalized = @(x0) (x0 - min(b.XLim))/diff(b.XLim) * pos(3) + pos(1);
xl = [x_normalized(2) x_normalized(2)]; % because you wanted x=2 m/s
yl = [0.1 0.2];
annotation('line',xl,yl,'Color',[1 0 0])

enter image description here