两个堆叠的 x 轴之间的垂直线
Vertical line between two stacked x-axes
我按照问题“”的解决方案创建了一个具有两个 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.
这是可行的,但很棘手。我使用 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])
我按照问题“
我现在正尝试在特定 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.
这是可行的,但很棘手。我使用 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])