【问题标题】:Vertical line between two stacked x-axes两个堆叠的 x 轴之间的垂直线
【发布时间】:2018-06-01 04:25:22
【问题描述】:

我按照问题“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.

【问题讨论】:

  • 你能说得更详细一点吗?在示例中,您提供的 x 轴具有不同的比例。你的比例一样吗?你可以附上你想要的例子吗? (只需在 Paint 中画出这条线)
  • 这是您真正应该考虑 Matlab 是否仍然是正确工具的地方。 Inkscape、Illustrator、tikz & co 应该可以为您节省大量时间。
  • 我希望在 x 轴上的特定点为非常大的数据集和大量图表创建这些线。如果您认为这在 MATLAB 中是不可能的,那么我会寻找其他程序。但我想先看看 MATLAB 是否有办法。
  • 因为 annotation 被规范化为图形窗口,而不是轴,您可能可以使用像 coord2norm 这样的辅助函数(免责声明,我写了这个)来生成 annotation 的 XY 对.

标签: matlab matlab-figure axes


【解决方案1】:

这是可行的,但很棘手。我使用了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])

【讨论】: