Clear Filters
Clear Filters

Second x-axis with non-uniform spacing

2 views (last 30 days)
Scott
Scott on 30 Jul 2020
Commented: Scott on 30 Jul 2020
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")
  1 Comment
Scott
Scott on 30 Jul 2020
I found a suitable method to add this data to the plot:
I used dataTipTextRow to add the information to each point.
So the code becomes:
s(1) = plot(x1,A,'+r')
row = dataTipTextRow('Other metric',otherMetricVector);
s(1).DataTipTemplate.DataTipRows(end+1) = row;

Sign in to comment.

Answers (0)

Categories

Find more on 2-D and 3-D Plots in Help Center and File Exchange

Products


Release

R2019a

Community Treasure Hunt

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

Start Hunting!