Multiple Axis Scales
Show older comments
I recall that Matlab is able to label a plot axis (say, x) with multiple scale markings (knots, miles per hour, meters per second) as "stacked" rows, but can't figure out where in the help docs it shows how to do this. For clarity
x-axis-----------------------------------------
Knots 0 50 100 etc
MPH 0 50 100 etc
m/s 0 25 50 etc
Also, I'd like to create a horizontal legend of more than one row (or vertical legend with more than one column). Any hints?
Any help is much appreciated.
Accepted Answer
More Answers (2)
Keith Lindsay
on 17 Aug 2011
0 votes
Terrance Frangakis
on 22 Feb 2018
Edited: Terrance Frangakis
on 22 Feb 2018
I have a simple workaround. In my case, I wanted the secondary x axis to have the inverse of values of the primary x axis (wavenumber on primary axis and wavelength on the secondary axis). The following example shows how I achieved this.
x1=[1:8];
y1=[1 6 2 4 3 1 2 3];
figure
plot(x1,y1);
ax1=gca;
grid
xlabel('Primary axis label')
ylabel('Value')
ax1_pos = get(ax1,'Position');
ax1_label=get(ax1,'XTick');
ax2 = axes('Position',ax1_pos,...
'XAxisLocation','top',...
'YTick',[],...
'XLim',[1 length(x1)],...
'XTickLabel',str2num(sprintf('%.2f ',1./ax1_label)),...
'XLabel','Secondary axis label',...
'Color','none');
The secondary axis needs to know how many xticks there are and what the xticklabels are. In my case, I used a simple element by element inversion, and formatted it to two decimal places. You can vary the number of ticks and labels. The number of labels can be altered by including the 'XTick' parameter in the axes command, by setting it to the same values as the primary axis 'XTick' array, and then changing the labels by the 'XTickLabel' parameter. The 'XTickLabel' array must have the same number of elements as the 'XTick' array.
For example, a new XTick array can be specified with additional entries, and the ax2 statement modified to add additional labels, as follows:
xt2=[1 1.5 2 2.5 3 3.5 4 4.5 5 5.5 6 6.5 7 7.5 8];
ax2 = axes('Position',ax1_pos,...
'XAxisLocation','top',...
'YTick',[],...
'XLim',[1 length(x1)],...
'XTick', [1 1.5 2 2.5 3 3.5 4 4.5 5 5.5 6 6.5 7 7.5 8],
'XTickLabel',str2num(sprintf('%.2f ',1./xt2)),...
'XLabel','Secondary axis label',...
'Color','none');
The labels may also be explicitly stated in the 'XTickLabel' parameter, rather than make use of a function, if that is more appropriate.
Categories
Find more on Axis Labels in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!