How do I show different x-axis up and down

4 views (last 30 days)
Erik
Erik on 26 Feb 2015
Commented: Star Strider on 26 Feb 2015
I'm am plotting the energy as a function of volume for a system and I want to show the volume on the x-axis down and the corresponding sidelength (i.e. the cubic root of the volume) on a similar x-axis top. The problem is that the cubic root produces a long series of decimals and I only want the ticks at the top x-axis in integer values of the side length, which is not equal to integer numbers for the volume. I.e. I don't want the ticks top and down to be at the same positions. So far I've come up with this:
plot(V, E), hold on L = V.^(1/3) axes('YAxisLocation','right','XAxisLocation','top','color','none') set(gca,'XTickLabel',L,'YTickLabel',[])
Any idea on how I might set the 'XTick' property on the top x-axis in such a way that the ticks coinside with integer values of L?

Answers (1)

Star Strider
Star Strider on 26 Feb 2015
Set the 'XTickLabel' values specifically:
V = logspace(1,3,5); % Create Data
L = V.^(1/3);
xlbl = strsplit(sprintf('%.0f\n', L), '\n');
xlbl = xlbl(1:end-1);
then:
set(gca,'XTickLabel',xlbl,'YTickLabel',[])
  2 Comments
Erik
Erik on 26 Feb 2015
Yes, that is a possibility, but that way I simply cut away the decimals, not shift the position of the tick to where L is an integer (and that is a quite noticable distance away).
Star Strider
Star Strider on 26 Feb 2015
I don’t know what ‘E’ is supposed to be, so I got creative.
In that event, define the 'XTick' positions as well, and round the ‘L’ values first:
V = logspace(1,3,5); % Create Data
E = V.^2;
L = V.^(1/3);
RL = round(L);
xlbl = strsplit(sprintf('%.0f\n', RL), '\n');
xlbl = xlbl(1:end-1);
plot(V, E)
set(gca, 'YAxisLocation','right','XAxisLocation','top','color','none')
set(gca,'XTick',RL.^3,'XTickLabel',xlbl,'YTickLabel',[])
You will probably have to experiment to get the result you want.

Sign in to comment.

Categories

Find more on Elementary Math in Help Center and File Exchange

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!