How can I show the time on Y axis on Bar graph or Stem graph with datetick ?

7 vues (au cours des 30 derniers jours)
Martinppp
Martinppp le 21 Fév 2018
Réponse apportée : Ian Townend le 7 Nov 2024 à 9:21
Hello,
I have a 20x1 cell array and on each cell there is a time in 'MM:SS.FFF'.
I use datenum to convert it in serial number. Now I would like to use a bar graph or stem graph with numeric values in X axis and those time values in Y Axis.
I use datetick after the plot to convert the value in time text but on my graph there is just '00:00.000' on the Y-axis and if I try to plot in plot graph it's working (I have my time values on Y axis).
My code is (with 'h' a 20x1 cell with time values in text):
Format = 'MM:SS.FFF';
z=[1 2 3 4 5 6 7 8 9 10 11 12 14 15 16 17 18 19 20 21];
h1= datenum(h,Format);
bar(z,h1)
datetick('y',Format)
Do you have a solution for this problem?
Thank you,
Martin

Réponses (1)

Ian Townend
Ian Townend le 7 Nov 2024 à 9:21
I have just been faced with the same problem. My solution was as follows:
%input y_values as datetime
startdate = min(y_values); %smallest datetime
plotvalues = y_values-startdate; %convert to durations
%create bar plot
bar(ax,x,plotvalues) %ax is handle to axes
y_ticks = ax.YTick; %get the plot ticks as durations
dtime = startdate+y_ticks; %convert to datetime
yticklabels(datestr(dtime)) %relabel as date strings
This was sufficient for my purposes.

Community Treasure Hunt

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

Start Hunting!

Translated by