File Exchange

image thumbnail

Add top X-axis with different scale

version 1.0 (3.29 KB) by

Add an X-axis on top (additional to the one on bottom) of the figure, with its own ticks labels ...

12 Ratings



View License

Add an X-axis on top (additional to the one on bottom) of the figure, with its own ticks labels and axis label (allow double scale).
Ticks, minor ticks positions and x limits are set to be identical for top and bottom axis. Moreover, they are linked, i.e. a modification of one of them, even after execution of this function, will result in modification of the other one.

Comments and Ratings (21)


Akila (view profile)

Thank you very much

Mallik Gurram


Ole (view profile)

Can you give an examle I can not make it work with pcolor plot .


BeGar (view profile)

Very useful!!! I only can set the axes labels by manual location of them.
and this error:

One or more output arguments not assigned during call to "evalin".

Error in ==> addTopXAxis at 132
set(nAxis, 'xtickLabel', num2str(evalin('base',
newXtickLabel_command), xTickLabelFormat));

Can anyone help me?


tbir (view profile)

I have found the solution - to plot everything in the old axes


tbir (view profile)

thank you for the useful function!
Could someone please give a short example how to connect an additional graph to the new horizontal axis?
just changing


creates a line directly on the new horizontal axis

thank you!

p kung

p kung (view profile)

Chern Hui Lee



Paul (view profile)

Very useful! Thanks a lot.

Massimo Ciacci

Massimo Ciacci (view profile)

After checking other solutions out there, this is by far the best and simple too, without complicate callback mechanisms to regenerate the labels on the secondary axis, which still zooms properly.

I had tried

set(gca, 'XTickMode', 'auto', 'XTickLabelMode', 'auto')

in the hope that labels would be updated automatically, but as a result the secondary axis becomes identical to the first!

Workaround, use manual xticks to start with, dense enough, before calling the function. But at least the new axis zooms properly! Well done Emmanuel!

Massimo Ciacci

Massimo Ciacci (view profile)

It's a pity that after the second axis is drawn the xticklabels are frozen, so if one zooms in, new labels are not generated...


Chiao (view profile)


Itamar (view profile)

First, thank you for your submission. Well documanted and written. It took me some time to figure it out, but it works like magic and as in the example.
You had really helped me here.

Pacome Delva

the script doesn't work for a loglog plot...

Pacome Delva

Hello, thank you for your contribution.
I have a problem. I want to add a top log axis to a loglog plot. My problem is that I don't know how to obtain a nice formatting, with the power in super script, as for the down x axis. I have only access to the fprinf option for the formatting.
Thank you

I agree with YI xu. (Somehow I cannot make it to function, has the evalin function been changed in MATLAB?)

Stian: axec() function? Did you mistype? Does not seem to be in MATLAB nor in 'fileexchange'.

YI xu

For a general user, I wonder if somebody can give an example statement, it's easier for users to understand. Thanks

Stian Ingebrigtsen

Why bother to use two manipulated Subplots when you can use the axec( ) function to generate a new set of opposite axis on your graph??? Your code will be a lot simpler and it is still possible to link axis properties...

Emmanuel Dinnat

Reply to John D'Errico
1st, thank you for the return.
The change has been done in the new version.
Also changed the way of evaluating the 'expression'. That should allow more complexe and easier implementation of these 'expressions'.

John D'Errico

I don't forsee a need a secondary x-axis much, but when I
want one, this is a good, simple tool for that purpose.

My only (minor) beef is with the property/value pair
interface. Its nice if properties can be shortened, as long as that shortening is unambiguous. Thus, the 'expression'
property should be properly used even if we supplied its
name as 'exp'. Since this takes only a couple of lines of
code to implement, its a very nice feature.


Now evaluates expression in the 'base' workspace (allow for more complex expressions), use case insensitive and simplified property names.

MATLAB Release
MATLAB 7.1.0 (R14SP3)

Inspired by: addtxaxis

Inspired: LinkTopAxisData

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