필터 지우기
필터 지우기

I have 24 hours data for one day. How to merge 3 hours into one bar plot?

조회 수: 2 (최근 30일)
MUHAMMAD
MUHAMMAD 2022년 12월 28일
댓글: MUHAMMAD 2022년 12월 30일
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
MUHAMMAD
MUHAMMAD 2022년 12월 29일
Since i cannot see the value because it stored in .mat file and it is very large file, i don't know how to apply your coding into my coding.
Can you download the Google Drive file and try to run it?
I want to get exactly like Figure C that have color for each bar that depends on their value.
Thank you!
dpb
dpb 2022년 12월 29일
편집: dpb 2022년 12월 29일
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,:);

댓글을 달려면 로그인하십시오.

채택된 답변

dpb
dpb 2022년 12월 29일
편집: dpb 2022년 12월 29일
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.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Bar Plots에 대해 자세히 알아보기

태그

제품


릴리스

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by