Discrepancy between datenum and datetime?
5 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Louise Wilson
il 20 Ott 2021
Modificato: Louise Wilson
il 21 Ott 2021
I am trying to plot some data (attached) .
The figure wasn't displaying the data I knew I had, so I tried again using datetime instead of datenum for the x-axis. These values are derived from the datenum data but for some reason they display the data correctly, whereas in the datenum plot, some data is missing and there is extra data in January... can anyone explain what is happening here?
sd_dn=datenum(2020,1,1,8,0,0); %date range in datetime
ed_dn=datenum(2020,6,7,17,0,0);
subplot(2,1,1)
title('time as datenum')
hold on
bar(y(:,1),y(:,2))
datetick('x','keepticks'); %change x axis to readable time
xlim([sd_dn, ed_dn]); %filter to date range of interest
subplot(2,1,2)
title('time as datetime')
hold on
y_dt=datetime(y(:,1),'ConvertFrom','datenum');
bar(y_dt,y(:,2))
xlim([(datetime(sd_dn,'ConvertFrom','datenum')), (datetime(ed_dn,'ConvertFrom','datenum'))]);
save('y.mat','y')
0 Commenti
Risposta accettata
Cris LaPierre
il 21 Ott 2021
It looks to me like there may be rendering issue here when the x values are datenums as opposed to datetimes. I added a scatter plot to show where the bars should be. You can see the second grouping is missing some bars, too.
load yDATENUMS.mat
bar(y(:,1),y(:,2))
hold on
scatter(y(:,1),y(:,2))
hold off
However, if I zoom in, the missing bars appear. If I zoom out, they disappear again.
figure
bar(y(:,1),y(:,2))
% zoom in to the missing side
xlim([737865 737951])
One small additional comment: I don't think the 'keepticks' option in datetick is doing what you think it is doing. I would leave it off. The x axes align better that way (in your orginal figure, Mar appears twice, for example).
sd_dn=datenum(2020,1,1,8,0,0); %date range in datetime
ed_dn=datenum(2020,6,7,17,0,0);
figure
subplot(2,1,1)
title('time as datenum')
bar(y(:,1),y(:,2))
datetick('x'); %change x axis to readable time
xlim([sd_dn, ed_dn]); %filter to date range of interest
subplot(2,1,2)
title('time as datetime')
y_dt=datetime(y(:,1),'ConvertFrom','datenum');
bar(y_dt,y(:,2))
xlim([(datetime(sd_dn,'ConvertFrom','datenum')), (datetime(ed_dn,'ConvertFrom','datenum'))]);
One last comment. These figures are rendering differently here that in desktop MATLAB. Be sure to test this on your computer to see what I am trying to explain.
1 Commento
Più risposte (1)
Dave B
il 21 Ott 2021
This is one of the many reasons we have datetime, and why we recommend using datetime instead of datenum. If you can use datetime, you should, it works better!
If you really must use datenum, here are a few workaround you can try (these both worked for me with your dataset/snippet). These very subtly change the appearance but maybe that's okay given how zoomed out the view is:
- Use edges instead of faces: bar(...,'FaceColor','none','EdgeColor','flat')
- Use a BarWidth of 1: bar(...,'BarWidth',1)
I suspect this is about floating point errors, and MATLAB is seeing the bars as 0 pixels wide and so not rendering them. Supporting this notion: the data aren't invisible with plot (or edges instead of faces), the bars can be seen if you zoom in far enough, and the bars are visible if you subtract y(1,1) from the x values in the datenum case.
Vedere anche
Categorie
Scopri di più su Dates and Time in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!