Bar plot doesn't accept width of 1.0

19 visualizzazioni (ultimi 30 giorni)
dormant
dormant il 5 Dic 2024 alle 13:37
Spostato: dpb il 5 Dic 2024 alle 15:07
This is really puzzling me.
I am plotting two datetime time series with the bar function, with the width set to 1.0. But one of the time series shows gaps between the bars.
Here is a test script and the plot. The files are all attached.
load testBar.mat;
figure;
bar( datetimeSeis, dataSeis, 1.0, 'r' );
hold on;
bar( datetimeOLD, dataOLD, 1.0, 'k' );
xlim( [ datetime(1995,1,1) datetime(2000,1,1) ] );
What am I doing wrong?

Risposta accettata

Rik
Rik il 5 Dic 2024 alle 13:58
As you can see, this behavior is caused by the x-values being dates. I suspect the underlying reason is that your data is not equally spaced, since your data is sampled once per month (and therefore the distance between each sample is not the same number of days (but 28, 30, or 31).
S=load('testBar.mat');
subplot(2,1,1)
bar(S.datetimeSeis,S.dataSeis, 1, 'r' )
subplot(2,1,2)
bar(1:numel(S.dataSeis),S.dataSeis,1,'r')
S.datetimeSeis(1:5)
ans = 1x5 datetime array
16-Jan-1995 16-Feb-1995 19-Mar-1995 19-Apr-1995 20-May-1995
You can over-compensate:
figure
bar(S.datetimeSeis,S.dataSeis, 1.75, 'r' )
  2 Commenti
dormant
dormant il 5 Dic 2024 alle 14:14
The distance between each sample is exactly 31 days.
>> datetimeSeis(1:10)
ans =
1×10 datetime array
16-Jan-1995 16-Feb-1995 19-Mar-1995 19-Apr-1995 20-May-1995 20-Jun-1995 21-Jul-1995 21-Aug-1995 21-Sep-1995 22-Oct-1995
dormant
dormant il 5 Dic 2024 alle 14:27
You are correct. The x values for datetimeOLD are all exactly 31 days apart. The last two x values for datetimeSeis are not 31 days apart.
My fault in my rebinning function.
Thank you very much.

Accedi per commentare.

Più risposte (1)

dpb
dpb il 5 Dic 2024 alle 15:06
Spostato: dpb il 5 Dic 2024 alle 15:07
load testBar.mat;
figure;
%bar( datetimeSeis, dataSeis, 1.0, 'r' );
%hold on;
bar( datetimeOLD, dataOLD, 1.0, 'k' );
%xlim( [ datetime(1995,1,1) datetime(2000,1,1) ] );
[datetimeSeis(1) datetimeSeis(end)]
ans = 1x2 datetime array
16-Jan-1995 18-Nov-2024
[datetimeOLD(1) datetimeOLD(end)]
ans = 1x2 datetime array
16-Jan-1995 04-Oct-2020
figure;
%bar( datetimeSeis, dataSeis, 1.0, 'r' );
hold on;
bar( datetimeOLD, dataOLD, 1.0, 'k' );
xlim( [ datetime(1995,1,1) datetime(2000,1,1) ] );
figure;
%bar( datetimeSeis, dataSeis, 1.0, 'r' );
hold on;
hB1=bar( datetimeOLD, dataOLD, 1.0, 'k' );
xlim( [ datetime(1995,1,1) datetime(2000,1,1) ] );
hB2=bar( datetimeSeis, dataSeis, 1.0, 'r' );
It appears to be a fignewton from when the second is put onto the same axes is added with such a wide disparity of dates; the Seis dataset goes all the way to 2024 where OLD only covers 2020; each looks ok individually.
figure;
isOld=(datetimeSeis<=datetimeOLD(end)); % keep only overlapping data
bar(datetimeSeis(isOld), dataSeis(isOld), 1.0, 'r' );
hold on;
bar(datetimeOLD, dataOLD, 1.0, 'k' );
xlim( [ datetime(1995,1,1) datetime(2000,1,1) ] );
AHA! -- That does the trick, indeed!

Categorie

Scopri di più su Line Plots in Help Center e File Exchange

Prodotti


Release

R2024b

Community Treasure Hunt

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

Start Hunting!

Translated by