Second x-axis with non-uniform spacing
    6 views (last 30 days)
  
       Show older comments
    
I have made a plot with two x-axes, however I am struggling to line up the x-axis.
I have some time series data (x), and an associated reading (y), each timestep/reading has an other metric (x1) associated with it.
I want to plot(x,y), and have x1 appear on a second x-axis.
The problem stems from the fact that the time step within my time series data is not constant.
I have attached some code/figure showcasing the problem, when the timestep is constant, the time series data and other metric line up perfectly.
When the timestep is non-uniform, they no longer line up.
Is there an elegant way to resolve this?
This is not a one-off plot, so I would prefer a more dynamic solution.
If you have any idea how to display the other metric besides a second x-axis, I'd be interested in that too.

close all
A = rand(100,1);
x1 = datetime('now') + minutes(1:100);
x2 = 101:200;
x3 = datetime('now');
for ii = 2:100
    x3(ii) = x3(ii-1) + minutes(rand());
end
figure();
subplot(2,1,1)
plot(x1,A,'+r')
xlabel("Time with uniform spacing")
ylabel("Random reading")
axis tight
hAx(1)=gca;
hAx(2)=axes('Position',hAx(1).Position,'XAxisLocation','top','color','none');
hold(hAx(2),'on')
plot(x2,A,'b')
axis tight
xlabel("Other metric with uniform spacing")
subplot(2,1,2)
plot(x3,A,'+r')
xlabel("Time with non-uniform spacing")
ylabel("Random reading")
axis tight
hAx(1)=gca;
hAx(2)=axes('Position',hAx(1).Position,'XAxisLocation','top','color','none');
hold(hAx(2),'on')
plot(x2,A,'b')
axis tight
xlabel("Other metric with uniform spacing")
Answers (0)
See Also
Categories
				Find more on Data Exploration 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!