matlabdatetimeplotbar-chartgraphic

Matlab - How to customize the xaxis with datetime values in a grouped bar graph?


How can I customize the xaxis for a grouped bar graph with datetime values:

I have a 150x3 matrice as my y-values and a vector of 150x1 datetimevalues in format dd-mm-yyyy hh:mm:ss. Each group of bars schould be labeled with one datetime value on the xaxis, as you can see in the picture: The enter image description here

Thank you!!!


Solution

  • clc; clear all; close all; 
    figure;
    y = [1 3 5; 3 2 7; 3 4 2];
    b = bar(y,'FaceColor','flat');
    d1 = '23-Aug-2010 16:35:42';
    t1 = datetime(d1,'InputFormat','dd-MMM-yyyy HH:mm:ss');
    d2 = '24-Aug-2010 16:35:42';
    t2 = datetime(d2,'InputFormat','dd-MMM-yyyy HH:mm:ss');
    d3 = '25-Aug-2010 16:35:42';
    t3 = datetime(d3,'InputFormat','dd-MMM-yyyy HH:mm:ss');
    x = [1 2 3];
    str = {datestr(t1); datestr(t2); datestr(t3)};
    set(gca, 'XTickLabel',str, 'XTick',1:numel(str));
    xtickangle(45);
    

    enter image description here