如何在 Matlab 绘图中插入两个 X 轴 [英] How to insert two X axis in a Matlab a plot
问题描述
我想创建一个具有相同绘图的双 X 轴(m/s 和 km/h)的 Matlab 图形.
I would like create a Matlab figure with a double X axis (m/s and km/h) with the same plot.
我找到了 plotyy 并且 - 在 Matlab 存储库中 - plotyyy,但我正在寻找:
I have found plotyy and - in Matlab reposity - plotyyy, but I am looking for:
- 双 X 轴.
- 一起在情节下方.
我的代码很简单:
stem(M(:, 1) .* 3.6, M(:, 3));
grid on
xlabel('Speed (km/h)');
ylabel('Samples');
M(:, 1)
是速度(单位为 m/s),M(:, 3)
是数据.
M(:, 1)
is the speed (in m/s), and M(:, 3)
is the data.
我只想要第二条线,在底部,速度以米/秒为单位.
I would like only a second line, in the bottom, with the speeds in m/s.
推荐答案
您可以执行以下操作.与 @Benoit_11 的解决方案相比,我确实使用了普通的 Matlab 标签并用句柄引用了两个轴,因此分配是明确的.
You can do something like the following. In comparison to the solution of @Benoit_11 I do use the normal Matlab labels and refer to both axes with handles so the assignments are explicit.
以下代码创建一个空的 x 轴 b
,单位为 m/s,高度可以忽略不计.在此之后,实际绘图绘制在第二个轴 a
中,该轴位于其他轴上方一点,单位为 km/h.要在特定轴上绘图,请插入轴句柄作为 stem
的第一个参数.从m/s 到km/h 的转换直接写在对stem
的调用中.最后,需要将两个轴的 xlim
属性设置为相同的值.
The following code creates an empty x-axis b
with the units m/s with a negligible height. After this, the actual plot is drawn in a second axes a
located a bit above the other axes and with units km/h. To plot on a specific axes, insert the axes-handle as the first argument of stem
. The conversion from m/s to km/h is directly written in the call to stem
. Finally, it's needed to set the xlim
-property of the both axes to the same values.
% 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 1e-12]);
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');
这篇关于如何在 Matlab 绘图中插入两个 X 轴的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!