File Exchange

image thumbnail


version 1.0 (3.93 KB) by

Adds a linked second x axis to a plot



No License

This function will add a second x axis to a plot with the top ticks at arbitrary positions (you are not limited to the bottom tick positions). The new axis is created behind the original axis, and the two axes are linked together. This gives a much more intuitive behaviour, for example clicking in the figure window will select the original axis. It also alows you to drag the plot around, resize the limits and change between a log/linear scale without having to update the dummy axis manually.

Comments and Ratings (11)

Works as described and with log-log plots. Nice work


Stefan (view profile)

Dear all,

I would like to ask for your help into improving this code. I am looking for the following functionality: when I zoom into the figure, the top x-axis does not automatically readjust the number of points, just like the bottom x-axis does. Any idea how to achieve this?

Thank you

Chad Greene

Chad Greene (view profile)

This function works well, especially after making the changes recommended by Christos are good. I also recommend adding this to the very end:

if nargout==0
clear newAxis

This will suppress output if the user does not want to create a new variable in the workspace.

Tim, thanks for sharing, and Christos, thanks for the suggestions.

p kung

p kung (view profile)

Aaron Vincent

Good job. Two comments

Comment 1: I think it is a good idea to change lines 46 and 49 as follows:
(46) temp=[TopTickPositions(:), TopTickLabels(:)];
(49) temp = sortrows(temp);
This will allow users to enter either row or column vectors (or both) for the ticks and labels.

Comment 2 (regarding the bug that Thierry reported):
It can be fixed if line 50 is replaced by
temp(diff(temp(:,1))==0,:) = [];


K R (view profile)

Thank you so much for this submission! It took me 15 minutes of playing around with it to figure it out (it wasn't immediately clear to me that TopTickPositions were the positions on the original axis and that TopTickLabels were the associated values on the new axis). But it works great! I really appreciate it - thanks!!

Thierry Dalon

small bug:
line temp=temp(diff(temp(:,1))>0,:);
will always remove on tick label.

Frederic Moisy

Works well, very useful for me. Thanks!

Tim Richards

Many thanks for your comment. I have changed the example and added a screenshot.

F Moisy

1. The example does not work (??? Undefined function or variable 'T')
2. This kind of submission typically requires a screenshot!

MATLAB Release
MATLAB 7.1.0 (R14SP3)

Inspired by: Add top X-axis with different scale

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video

Win prizes and improve your MATLAB skills

Play today