Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MATLAB: Plotting double bottom x-axis

I'm trying to plot double bottom x-axis according to the figure after the jump:

enter image description here

It is a semilogx-plot in hours with additional specific ticks representing minutes, months and years. How do I create these additional ticks?

like image 480
user3585594 Avatar asked Apr 29 '14 14:04

user3585594


People also ask

How do you plot a double X axis in MATLAB?

For example, you can create two plots that have different x- and y-axis limits. First, create two sets of x- and y-coordinates. x1 = 0:0.1:40; y1 = 4. *cos(x1)./(x1+2); x2 = 1:0.2:20; y2 = x2.

How do you plot multiple X axis in origin?

From the main menu, select Plot > Multi-Panel/Axis : 4Ys YY-YY to create a plot as shown below: Double-click on the X axis to open the X Axis - Layer 1 dialog box. Click the Reference Lines tab, then click the browse button to the right of the Reference Lines at Value box.

What is Plotyy MATLAB?

plotyy(X1,Y1,X2,Y2,function) uses the specified plotting function to produce the graph. function can be either a function handle or a character vector specifying plot , semilogx , semilogy , loglog , stem , or any MATLAB® function that accepts the syntax. h = function(x,y)


1 Answers

You can use Victor May's approach to overlay a secondary axes, but this needs more tweaking to actually work.

First, let's recreate the basic plot:

clear, close
time = [1/60, 1, 24, 24*7, 24*30, 24*30*6, 24*365, 24*365*10];
r = [110, 90, 80, 75, 70, 65, 63, 60];

% plot the data
semilogx(time, r, 'o-')

% adjust ticks and format of primary axes
xlim([0.005 1e6])
ylim([0 140])
tick = 10 .^ (-2 : 6);
set(gca, 'XTick', tick)
set(gca, 'XTickLabel', arrayfun(@num2str, tick, 'UniformOutput', false))
set(gca, 'XMinorTick', 'off')
set(gca, 'TickDir', 'out')

enter image description here

Overlaying a secondary axes only works properly if it has the same position, size, axis limits, and scale type as the primary, and if its background is transparent (otherwise the data become hidden):

% put matching secondary axes on top with transparent background
pos = get(gca, 'Position');
axes('Position', pos)
set(gca, 'Color', 'none')
xlim([0.005 1e6])
ylim([0 140])
set(gca, 'XScale', 'log')
set(gca, 'XMinorTick', 'off')
set(gca, 'TickDir', 'out')

Giving it the proper ticks and tick labels

% adjust ticks
set(gca, 'YTick', [])
set(gca, 'XTick', time)
label = {'1 min', '1 hour', '24 hours', '1 week', '1 month', '6 months', '1 year', '10 years'};
set(gca, 'XTickLabel', label)

results in

enter image description here

– not really what we want.

With a trick we can let the ticks and tick labels of the secondary axes go inside...

% tinker with it
set(gca, 'XAxisLocation', 'top')
pos(4) = eps * pos(4);
set(gca, 'Position', pos)

enter image description here

...but that's still not really what we want.


A different strategy: Let's not overlay axes, but put the additional ticks in ourselves!

label = {'1 min', '1 hour', '24 hours', '1 week', '1 month', '6 months', '1 year', '10 years'};
line([time', time'], [0 2], 'Color', 'k')
text(time, 4 * ones(size(time)), label, 'Rotation', 90, 'VerticalAlignment', 'middle')

The result

enter image description here

is still not perfect, but useable.

like image 67
A. Donda Avatar answered Sep 28 '22 11:09

A. Donda