Second x-axis with non-uniform spacing

조회 수: 7 (최근 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에서 2-D and 3-D Plots에 대해 자세히 알아보기

제품


릴리스

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by