Azzera filtri
Azzera filtri

Axis date values disappear when datetick is applied to a simple time-series plot

5 visualizzazioni (ultimi 30 giorni)
Hello,
I'm creating a plot of time (x-axis) against flow rate (y-axis). Time is displayed in decimal format and I want to convert this to real dates in the format dd/mm. To do this I am using datetick, which normally works for me, but the result in this instance is that all values on the x-axis are removed with the exception of the first and last values (which are converted to dd/mm format). Seems simple.
figure; plot(Time,flowrate,'r.'); datetick('x','dd/mm');
Any suggestions to what I might be doing wrong would be greatly appreciated.

Risposta accettata

Harry MacDowel
Harry MacDowel il 7 Set 2011
I feel your pain. I used to deal with date on x-axis and it is really a huge, huge pain.
I suggest you go to File Exchange and grab the function called: xticklabelrotate
That function can actually rotate your x-axis at any angle you want. For dates, I go for a 90 degrees rotation.
Using datetick straight away will create a lot of problems. For me, I am tired of doing that so I experimented my own way. Somehow I achieved great feats with the following procedure:-
1) Convert your time matrix into strings using datestr BEFORE you plot it. Or below I provide my way of doing it which is doing it on the spot. Change the codes the way you want;
2) Determine how many x-ticks you want. If you want 21 x-ticks, then generate a so-called spaceData as follows:-
spaceData = linspace(1,x_lim,20);
spaceData = round(spaceData);
Here my x_lim is the last of my Time matrix. It is acquired by:-
x_lim = length(time);
3)Then create the xData:-
for i=1:20
xData(i) = cellstr(datestr(time(spaceData(i))));
end
What I am doing here is changing the time value at the locations notified by spaceData into strings. Then 'trap' them into cellstring.
4)And then I generate the following two matrices:
xdata_gen = zeros(x_lim,1);
for i=1:x_lim
xdata_gen(i) = i;
end
Mind that x_lim here is not 21 but the length of the time matrix. What I am doing here is generating the exact number of xdata_gen elements as that of the variable time.
xdata_gen is just a number matrix from 1,2,3,4,5......x_lim
5. Here comes the magic:-
hl1 = bar(xdata_gen,y);
ax1 = gca;
set(gca,'color','white'); % sets the color to white
set(gca,'Box','off'); % here gca means get current axis
set(gca,'TickDir','out');
set(gca,'XTick',spaceData,'XTickLabel',xData,'XMinorTick','on','YMinorTick','on','XLim',[0 x_lim])
xticklabel_rotate;
So the trick is to plot the numbered matrix against y (or your flowrate, then tell Matlab that you only want 21 ticks and their positions on x-axis.
After that change all the numbers into the cellstr date matrix.
After that rotate the x-axis labels.
Sounds a little complicated but it really works.
Good luck!
  3 Commenti
Oleg Komarov
Oleg Komarov il 7 Set 2011
It doesn't need to be so complicated as I shown below. Please read also my comment below.
Harry MacDowel
Harry MacDowel il 7 Set 2011
No worries Saul. Hope it really helps. =)
The magic of my method is the fact that you can project it to plot multiple x-axis but only show the date and not double x-axis.
You can create a matrix equivalent to the length of time matrix called emptyData in which all are the cellstr(' ')
Pad the emptyData onto the newly laid over x-axis of the plot.
Cheers!

Accedi per commentare.

Più risposte (1)

Oleg Komarov
Oleg Komarov il 7 Set 2011
Get used to set the properties for your plots, especially when you start adding a little bit of customization. My plots sometimes take the whole screen and I am not doing anything special apart from setting labels, ticks and manually subplotting.
An example:
time = now-99:now;
flow = cumsum(randn(100,1));
plot(time,flow,'r')
datetick('x','dd/mm');
% Set Xlim and ticks position
set(gca, 'Xlim',time([1,end]),'Xtick',linspace(time(1),time(end),9))
  2 Commenti
Saul
Saul il 7 Set 2011
Thanks Oleg, for some reason this didn't work with my plot, it adds the extra dates as needed but the only two dates are replicated 9 times.
Thanks anyway
Oleg Komarov
Oleg Komarov il 7 Set 2011
Without the actual data is hard to guess what is happening. I would be interested to know what your times is.

Accedi per commentare.

Categorie

Scopri di più su Line Plots 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!

Translated by