I have 24 hours data for one day. How to merge 3 hours into one bar plot?
2 views (last 30 days)
Show older comments
Hello admin,
I would like to create a bar plot exactly like shown in Figure C. Before that, I'm so sorry, this is so confusing but i will explain it one by one.
This is what I have created in Excel, not in Matlab.
As you can see,
- figure A (before) is one-day data with 1-hour interval of 24 hours (x-label) bar graph
- figure A (after) is one-day data with 3-hour interval of 24 hours (x-label) bar graph
Initially, i have 24-hours data that have same value for each 3-hours interval as shown in Figure A (before). Therefore, i want to merge same bar, same value into one big bar as shown in Figure A (after). But i have no idea how to merge it since i'm new in this Matlab course :(
But i have the data like exactly Figure B which contains 5-days bar graph in one plot continuously.
I've already attached the .m file named (BarGraph.m) in Google Drive for you to try. Link : Google Drive Data
So what i want is,
- To merge the bar in Figure A (before) to Figure A (after) but in the a Figure B coding (BarGraph.m file)?
Then, if success, how to color each bar based on their value exactly like Figure C?
This is my idea to color based on the value
if k > 6
set(h,'FaceColor','r'); %red, k >= 6
elseif k == 5
set(h,'FaceColor','y'); %yellow, k = 5
else
set(h,'FaceColor','g'); %green, k < 5
end
Please i dont have any idea :( Thankyou!
3 Comments
dpb
on 29 Dec 2022
Edited: dpb
on 29 Dec 2022
Post here on the Answers site, not external sites.
Just load your .mat file instead of using readmatrix and substitute your variable name for 'data'
If it is an array instead of a vector, then add the second dimension (:) to get all columns --
load YourMatfile
yourVariableName=yourVariableName(1:3,:);
Accepted Answer
dpb
on 29 Dec 2022
Edited: dpb
on 29 Dec 2022
Try the following for starters...
data=repmat(randi(10,50,1),1,3).'; data=data(:); % data similar to yours with 3 repeats
data1=data(1:3:end); % select only one of the three
subplot(3,1,1) % illustration of what we get
bar(data)
ylim([0 12])
title('Full Data Set')
subplot(3,1,2)
bar(data1)
ylim([0 12])
title('Reduced Data Set')
% now make fancy one...
ix=discretize(data1,[0 5 6 inf]); % group by the desired breakpoints
c={'g','y','r'}; % colors for the above groups
nG=max(ix); % how many groups there are
subplot(3,1,3)
hold on
box on
ylim([0 12])
title('Reduced Data Set by Group')
x=1:numel(data1); % an x vector to use to place the bars horizontally
nG=max(ix);
for i=1:nG
y=nan(size(x)); % to have full length vector of y so bar will keep same scaling
iz=(ix==i);y(iz)=data1(iz); % pick the ones in the given group only; rest nan
hB(i)=bar(x,y,c{i}); % and bar() them each in turn in right places, color
end
Unfortunately, bar() isn't amenable to addressing the individual bars in an overall vector; it can be done but is more work because have to go to the CData property that uses only RGB triplets; more often it's easier to just cobble up the data and add a separate bar() object for each special effect desired.
I've railed about the miserable user interface/design of bar() for 30+ years to minimal effect; some features have been added, but the basic function is still extremely difficult to use.
More Answers (0)
See Also
Categories
Find more on Bar 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!