Second x-axis with non-uniform spacing

2 次查看(过去 30 天)
Scott
Scott 2020-7-30
评论: Scott 2020-7-30
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 个评论
Scott
Scott 2020-7-30
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;

请先登录,再进行评论。

回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Data Distribution Plots 的更多信息

产品


版本

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by