找到你要的答案

Q:How to insert two X axis in a Matlab a plot

Q:如何插入两个X轴在MATLAB的阴谋

I would like create a Matlab figure with a double X axis (m/s and km/h) with the same plot.

I have found plotyy and - in Matlab reposity - plotyyy, but I am looking for:

  1. A double X axis.
  2. Together below the plot.

My code is very simple:

stem(M(:, 1) .* 3.6, M(:, 3));

grid on

xlabel('Speed (km/h)');
ylabel('Samples');

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.

我想有一双X轴创建MATLAB图形(m/s和km/h)具有相同的情节。

我发现在Matlab库- plotyy和plotyyy,但是我在寻找:

  1. A double X axis.
  2. Together below the plot.

我的代码很简单:

stem(M(:, 1) .* 3.6, M(:, 3));

grid on

xlabel('Speed (km/h)');
ylabel('Samples');

M(:1)是速度(M / s),m(,(3))是数据。

我希望只有第二行,在底部,与速度在米/秒。

answer1: 回答1:

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.

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');

你可以做如下的事情。在解决“benoit_11我使用MATLAB的标签和参考轴线,用手柄使任务明确的比较。

下面的代码创建一个空的X轴B与单位米/秒,可以忽略不计的高度。在此之后,实际的图绘制在第二轴位于一个位以上的其他轴和单位公里/小时。在特定的轴上绘制,插入轴柄作为第一个参数的干。从M / s到KM / h的转换直接写在调用干。最后,我们需要设置的两轴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');
answer2: 回答2:

As a very simple alternative you could also create a 2nd axis (transparent) and put it below the first one so that you only see the x axis.

Example:

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')

Output:

Then you can manually add the x labels for each unit, in different color for example.

作为一个非常简单的选择,你也可以创建一个第二轴(透明),并把它放在第一个,这样你只看到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标签为每个单位,在不同的颜色,例如。

answer3: 回答3:

The best way i can think to do it is to use 2 plots, for example, you can split the plot into a large and small section by doing something like this:

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

This is untested, you might need to fiddle around a little but it should hopefully put you on the right track.

我能想到的最好的方法是使用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

这是考验,你可能需要玩一点但它应该能让你在正确的轨道上。

matlab  plot  matlab-figure  axis-labels