Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to insert two X axis in a Matlab a plot

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.

like image 290
Giacomo Alessandroni Avatar asked Jun 23 '15 17:06

Giacomo Alessandroni


People also ask

How do I make two X axes in MATLAB?

Create an axes object ax2 by calling the axes function and specifying t as the parent object. Plot x2 and y2 as a black line, and specify ax2 as the target axes. Move the x-axis to the top, and move the y-axis to the right. Set the color of the axes object to 'none' so that the underlying plot is visible.

How do I add a secondary axis to a plot in MATLAB?

Plot Data Using Two y-Axes Plot a set of data against the left y-axis. Then, use yyaxis right to activate the right side so that subsequent graphics functions target it. Plot a second set of data against the right y-axis and set the limits for the right y-axis.

How do you plot multiple x-axis in origin?

From the main menu, select Plot > Multi-Panel/Axis : 4Ys YY-YY to create a plot as shown below: Double-click on the X axis to open the X Axis - Layer 1 dialog box. Click the Reference Lines tab, then click the browse button to the right of the Reference Lines at Value box.

How do you plot 3 axis in MATLAB?

plot3( X , Y , Z ) plots coordinates in 3-D space. To plot a set of coordinates connected by line segments, specify X , Y , and Z as vectors of the same length. To plot multiple sets of coordinates on the same set of axes, specify at least one of X , Y , or Z as a matrix and the others as vectors.


1 Answers

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.

Example Plot

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');
like image 154
Matt Avatar answered Sep 28 '22 03:09

Matt