how to visualize histogram with Datatime on Axis
10 次查看(过去 30 天)
显示 更早的评论
i want to visualize similar but using histogram
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1475671/image.png)
0 个评论
回答(1 个)
Steven Lord
2023-9-7
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 个评论
Steven Lord
2023-9-7
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)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Data Distribution Plots 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!