# Multiple X axis in different scale, but one y axis

38 views (last 30 days)
Alpha Boy on 27 Jul 2020
Commented: Alpha Boy on 17 Aug 2020
Hello,

Adam Danz on 28 Jul 2020
Edited: Adam Danz on 28 Jul 2020
Matlab does not support mutliple x axes other than the axes at the upper and lower borders of the plot.
One way to include multiple axis units is to create multi-lined x-axis ticks. The upper axis will be your main axis used to plot your data. Then define the axis limits for all additional pseudo-axes and compute the tick values for each pseudo-axis so they align with the x-ticks on the main axis. Use those value to create the multi-lined x-tick labels.
Demo
% Set up main axis (on top)
clf()
ax = axes();
ax.XTick = 0:200:2800;
ax.XLim = [0,2800];
% Set axis limits for all other axes
0, 1400; % axis 2
19.2, 23.4; % axis 3
0.2, 3]; % axis 4
% Compute tick values for each axis to align with main axis
% Set up multi-line ticks
tickLabels = compose('%4d\\newline%4d\\newline%.1f\\newline%.1f', allTicks(:).');
% The %4d adds space to the left so the labels are centered.
% You'll need to add "%.1f\\newline" for each row of labels (change formatting as needed).
% Alternatively, you can use the flexible line below that works with any number
% of rows but uses the same formatting for all rows.
% tickLabels = compose(repmat('%.2f\\newline',1,size(allTicks,1)), allTicks(:).');
% Decrease axis height & width to make room for labels
ax.Position(3:4) = ax.Position(3:4) * .75; % Reduced to 75%
ax.Position(2) = ax.Position(2) + .2; % move up
set(ax, 'XTickLabel', tickLabels, 'TickDir', 'out')
% Define each row of labels
ax2 = axes('Position',[sum(ax.Position([1,3]))*1.08, ax.Position(2), .02, 0.001]);
axisLabels = {'Distance (t)','Area (m^2)','Cnt from 0 (m)','KB (m)'}; % one for each x-axis
set(ax2,'XTick',0.5,'XLim',[0,1],'XTickLabel', strjoin(axisLabels,'\\newline'))
ax2.TickLength(1) = 0.2; % adjust as needed to align ticks between the two axes
Similar examples in the forum

Alpha Boy on 28 Jul 2020
Sir, Please help me. I am not requesting you to do my project. I simply want to learn. I have stuck with this for last 3 days
Adam Danz on 28 Jul 2020
"in this line, what does "(1)" mean?"
Look at the value of ax.XLim. The (1) selects the first element which is the lower x-limit.
Normalization is when you convert a set of data to a different scale. A well known example is percentages. 0.2, 0.5, 0.8 can be converted to 20%, 50%, 80% by multiplying by 100. Your normalization is a bit more complicated because the data must be scaled to fit within 4000-30000.
"And where I have to write this codes, I mean which lines? at the end?"
You can normalize the data anywhere in your code after the ax.XLim is set.
I suggest doing the plotting after the axes are all set up.
Alpha Boy on 17 Aug 2020
Thanks a lot sir