How can we change each bar in bar chart in MATLAB

1 view (last 30 days)
Hello,
I have a bar chart in Matlab. and I want to change every 24 bar to the same color. For example, the first 24 of them to the red, the second 24 of them to the blue, the third 24 of them to green, and the fourth 24 of them to black, but the color of the legend and the xlabel is incorrect. Here is my code:
data_kind2=xlsread('kind2.xlsx');
colors={'r','g','b','k'};
figure();
hold on
bar(data_kind2)
for ii=0:3
bar(ii*24+(1:24),data_kind2(ii*24+(1:24)),'FaceColor',colors{ii+1})
end
legend('Summer','Autumn','Winter','Spring');
set(gca,'Xtick',1:1:96,'XTickLabel',{'Evapotranspiration Deforestation Areas','Evapotranspiration Forest Areas','Evapotranspiration Transition Areas',...
'Soil moisture Deforestation Areas','Soil moisture Forest Areas','Soil moisture Transition Areas',...
'Air Temperature Deforestation Areas','Air Temperature Forest Areas','Air Temperature Transition Areas',...
'Total Runoff Deforestation Areas','Total Runoff Forest Areas','Total Runoff Transition Areas',...
'Sensible Heat Flux Deforestation Areas','Sensible Heat Flux Forest Areas','Sensible Heat Flux Transition Areas',...
'Soil Temperature Deforestation Areas','Soil Temperature Forest Areas','Soil Temperature Transition Areas',...
'ET/P Deforestation Areas','ET/P Forest Areas','ET/P Transition Areas','R/P Deforestation Areas','R/P Forest Areas','R/P Transition Areas',...
'Evapotranspiration Deforestation Areas','Evapotranspiration Forest Areas','Evapotranspiration Transition Areas',...
'Soil moisture Deforestation Areas','Soil moisture Forest Areas','Soil moisture Transition Areas',...
'Air Temperature Deforestation Areas','Air Temperature Forest Areas','Air Temperature Transition Areas',...
'Total Runoff Deforestation Areas','Total Runoff Forest Areas','Total Runoff Transition Areas',...
'Sensible Heat Flux Deforestation Areas','Sensible Heat Flux Forest Areas','Sensible Heat Flux Transition Areas',...
'Soil Temperature Deforestation Areas','Soil Temperature Forest Areas','Soil Temperature Transition Areas',...
'ET/P Deforestation Areas','ET/P Forest Areas','ET/P Transition Areas','R/P Deforestation Areas','R/P Forest Areas','R/P Transition Areas'
'Evapotranspiration Deforestation Areas','Evapotranspiration Forest Areas','Evapotranspiration Transition Areas',...
'Soil moisture Deforestation Areas','Soil moisture Forest Areas','Soil moisture Transition Areas',...
'Air Temperature Deforestation Areas','Air Temperature Forest Areas','Air Temperature Transition Areas',...
'Total Runoff Deforestation Areas','Total Runoff Forest Areas','Total Runoff Transition Areas',...
'Sensible Heat Flux Deforestation Areas','Sensible Heat Flux Forest Areas','Sensible Heat Flux Transition Areas',...
'Soil Temperature Deforestation Areas','Soil Temperature Forest Areas','Soil Temperature Transition Areas',...
'ET/P Deforestation Areas','ET/P Forest Areas','ET/P Transition Areas','R/P Deforestation Areas','R/P Forest Areas','R/P Transition Areas'
'Evapotranspiration Deforestation Areas','Evapotranspiration Forest Areas','Evapotranspiration Transition Areas',...
'Soil moisture Deforestation Areas','Soil moisture Forest Areas','Soil moisture Transition Areas',...
'Air Temperature Deforestation Areas','Air Temperature Forest Areas','Air Temperature Transition Areas',...
'Total Runoff Deforestation Areas','Total Runoff Forest Areas','Total Runoff Transition Areas',...
'Sensible Heat Flux Deforestation Areas','Sensible Heat Flux Forest Areas','Sensible Heat Flux Transition Areas',...
'Soil Temperature Deforestation Areas','Soil Temperature Forest Areas','Soil Temperature Transition Areas',...
'ET/P Deforestation Areas','ET/P Forest Areas','ET/P Transition Areas','R/P Deforestation Areas','R/P Forest Areas','R/P Transition Areas'});
set(gca,'XTickLabelRotation',90)
title('Relative Change v.s Monthly water balance in Deforestation,Forest, and Transition areas');
ylabel('Relative Change <%>')
xlabel('Monthly water balance');
grid on
And here is my error:
Error using k2 (line 10)
Dimensions of matrices being concatenated are not consistent.
Could anybody help me?
  2 Comments
dpb
dpb on 12 Jun 2021
...
hold on
bar(data_kind2)
for ii=0:3
bar(ii*24+(1:24),data_kind2(ii*24+(1:24)),'FaceColor',colors{ii+1})
end
...
You have drawn five separate bar() plots on the axes, but not saved the handles to any of them so you have no way to address a specific set or subset of each and thereby set a legend object to match a given object handle.
Look at the documentation for bar(); it has examples for making changes on individual bar properties.
nazanin tavakoli
nazanin tavakoli on 13 Jun 2021
Could you please explain more? I have run my code completely when I had just 24 numbers and I changed the color of every 6 of them, here is my code:
data_forest=xlsread('for.xlsx');
figure
bar(data_forest)
colors={'r','g','b','k'};
figure();
hold on;
for ii=0:3
bar(ii*6+(1:6),data_forest(ii*6+(1:6)),'FaceColor',colors{ii+1})
end
legend('Summer','Autumn','Winter','Spring');
set(gca,'Xtick',1:1:24,'XTickLabel',{'Evapotranspiration','Soil moisture',' Air Temperature','Total Runoff','Sensible Heat Flux','Soil Temperature',...
'Evapotranspiration','Soil moisture',' Air Temperature','Total Runoff','Sensible Heat Flux','Soil Temperature',...
'Evapotranspiration','Soil moisture',' Air Temperature','Total Runoff','Sensible Heat Flux','Soil Temperature',...
'Evapotranspiration','Soil moisture',' Air Temperature','Total Runoff','Sensible Heat Flux','Soil Temperature'});
set(gca,'XTickLabelRotation',90)
title('Relative Change v.s Monthly water balance in Forest area');
ylabel('Relative Change <%>')
xlabel('Monthly water balance');
grid on
ylim([-50 40])
And now I want to change the color of every 24 bars and I have 96 bars.
Could you please give me more hint?

Sign in to comment.

Answers (0)

Categories

Find more on Agriculture in Help Center and File Exchange

Products


Release

R2015b

Community Treasure Hunt

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

Start Hunting!