Second x-axis with non-uniform spacing

7 vues (au cours des 30 derniers jours)
Scott
Scott le 30 Juil 2020
Commenté : Scott le 30 Juil 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 commentaire
Scott
Scott le 30 Juil 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;

Connectez-vous pour commenter.

Réponses (0)

Catégories

En savoir plus sur 2-D and 3-D Plots dans Help Center et File Exchange

Produits


Version

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by