how to visualize histogram with Datatime on Axis
13 views (last 30 days)
Show older comments
i want to visualize similar but using histogram
0 Comments
Answers (1)
Steven Lord
on 7 Sep 2023
You can call histogram with datetime data as the data to be binned. Let's make some random data for the first half of today to use for the example.
sz = [100 1];
dt = datetime('today') + ...
hours(randi([0 11], sz)) + ...
minutes(randi([0 60]));
Specify the bin edges as each hour from midnight to noon.
edges = datetime('today') + hours(0:12);
Create the histogram and set the x axis ticks to be every other hour from the edges vector. Specifying every hour IMO made the axes a bit too "busy".
histogram(dt, edges)
xticks(edges(1:2:end))
3 Comments
Steven Lord
on 7 Sep 2023
Okay, so you don't actually want to call histogram but you want a plot that looks like a histogram? In that case I'd look through the Plot Gallery (or the Plots tab on the Toolstrip in your MATLAB installation) to find a plot that looks like what you're trying to create.
In the Plot Gallery you can Open Example to open an example file that shows how to create plots of that type then copy and adapt the code in those examples to suit your data and needs.
In the Plots tab in the Toolstrip, look for the name of the function underneath the thumbnail picture and open the documentation for that function. Alternately if you've selected the data you want to visualize in the Workspace window you can directly click on the thumbnail and MATLAB will create that type of plot using your data.
I suspect the bar function will do what you want, and it too supports specifying datetime data as x data.
x = datetime('today') + days(0:9);
y = (1:10).^2;
bar(x, y)
See Also
Categories
Find more on Data Distribution Plots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!