如何在 Matlab 绘图中插入两个 X 轴
How to insert two X axis in a Matlab a plot
我想创建一个具有相同绘图的双 X 轴(m/s 和 km/h)的 Matlab 图。
我在 Matlab 存储库中找到了 plotyy 和 - plotyyy,但我正在寻找:
- 双 X 轴。
- 一起下图。
我的代码很简单:
stem(M(:, 1) .* 3.6, M(:, 3));
grid on
xlabel('Speed (km/h)');
ylabel('Samples');
M(:, 1)
是速度(在m/s),M(:, 3)
是数据。
我只想要第二行,在底部,速度在 m/s。
我能想到的最好的方法是使用 2 个地块,例如,您可以通过执行以下操作将地块分成一大一小部分:
subplot(100, 1, 1:99) // plot your graph as you normally would
plot(...
subplot(100, 1, 100) // Plot a really small plot to get the axis
plot(...)
b = axis()
axis([b(1:2), 0, 0]) // set the y axis to really small
这是未经测试的,您可能需要 fiddle 一点点,但它应该能让您走上正确的轨道。
作为一个非常简单的替代方法,您还可以创建第二个轴(透明)并将其放在第一个轴下方,这样您就只能看到 x 轴。
示例:
clear
clc
close all
x = 1:10;
x2 = x/3.6;
y = rand(size(x));
hP1 = plot(x,y);
a1Pos = get(gca,'Position');
%// Place axis 2 below the 1st.
ax2 = axes('Position',[a1Pos(1) a1Pos(2)-.05 a1Pos(3) a1Pos(4)],'Color','none','YTick',[],'YTickLabel',[]);
%// Adjust limits
xlim([min(x2(:)) max(x2(:))])
text(2.85,0 ,'m/s','FontSize',14,'Color','r')
text(2.85,.05 ,'km/h','FontSize',14,'Color','r')
输出:
然后您可以手动为每个单元添加 x 标签,例如不同颜色。
您可以执行如下操作。与 @Benoit_11 的解决方案相比,我确实使用了普通的 Matlab 标签并引用了带有句柄的两个轴,因此分配是明确的。
以下代码创建了一个空的 x 轴 b
,单位为 m/s,高度可以忽略不计。在此之后,实际绘图绘制在第二个轴 a
中,位于其他轴上方一点,单位为 km/h。要在特定轴上绘图,请插入轴句柄作为 stem
的第一个参数。从m/s到km/h的转换直接写在对stem
的调用中。最后,需要将两个轴的xlim
-属性设置为相同的值。
% 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');
我想创建一个具有相同绘图的双 X 轴(m/s 和 km/h)的 Matlab 图。
我在 Matlab 存储库中找到了 plotyy 和 - plotyyy,但我正在寻找:
- 双 X 轴。
- 一起下图。
我的代码很简单:
stem(M(:, 1) .* 3.6, M(:, 3));
grid on
xlabel('Speed (km/h)');
ylabel('Samples');
M(:, 1)
是速度(在m/s),M(:, 3)
是数据。
我只想要第二行,在底部,速度在 m/s。
我能想到的最好的方法是使用 2 个地块,例如,您可以通过执行以下操作将地块分成一大一小部分:
subplot(100, 1, 1:99) // plot your graph as you normally would
plot(...
subplot(100, 1, 100) // Plot a really small plot to get the axis
plot(...)
b = axis()
axis([b(1:2), 0, 0]) // set the y axis to really small
这是未经测试的,您可能需要 fiddle 一点点,但它应该能让您走上正确的轨道。
作为一个非常简单的替代方法,您还可以创建第二个轴(透明)并将其放在第一个轴下方,这样您就只能看到 x 轴。
示例:
clear
clc
close all
x = 1:10;
x2 = x/3.6;
y = rand(size(x));
hP1 = plot(x,y);
a1Pos = get(gca,'Position');
%// Place axis 2 below the 1st.
ax2 = axes('Position',[a1Pos(1) a1Pos(2)-.05 a1Pos(3) a1Pos(4)],'Color','none','YTick',[],'YTickLabel',[]);
%// Adjust limits
xlim([min(x2(:)) max(x2(:))])
text(2.85,0 ,'m/s','FontSize',14,'Color','r')
text(2.85,.05 ,'km/h','FontSize',14,'Color','r')
输出:
然后您可以手动为每个单元添加 x 标签,例如不同颜色。
您可以执行如下操作。与 @Benoit_11 的解决方案相比,我确实使用了普通的 Matlab 标签并引用了带有句柄的两个轴,因此分配是明确的。
以下代码创建了一个空的 x 轴 b
,单位为 m/s,高度可以忽略不计。在此之后,实际绘图绘制在第二个轴 a
中,位于其他轴上方一点,单位为 km/h。要在特定轴上绘图,请插入轴句柄作为 stem
的第一个参数。从m/s到km/h的转换直接写在对stem
的调用中。最后,需要将两个轴的xlim
-属性设置为相同的值。
% 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');