How can we change each bar in bar chart in MATLAB

12 visualizzazioni (ultimi 30 giorni)
nazanin tavakoli
nazanin tavakoli il 12 Giu 2021
Risposto: Arjun il 26 Feb 2025
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 Commenti
dpb
dpb il 12 Giu 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 il 13 Giu 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?

Accedi per commentare.

Risposte (1)

Arjun
Arjun il 26 Feb 2025
I understand that you want to create a MATLAB bar chart with 96 bars, coloring every 24 bars distinctly (red, blue, green, black) and ensuring the legend accurately reflects these color groups.
In the current code, the problem is that the bar handles are not stored, which leads to a mismatched legend that does not align with the bar colors. Without these handles, it's tough to correctly link each color group to its legend entry. The fix is simple, store the bar handles when plotting each set of 24 bars, so the legend accurately reflects the colors you've assigned. While making the legend provide these bar handles with the labels to get accurate solution.
Kindly refer to the modified code below:
data_kind2 = randn(96, 1) * 10;
colors = {'r', 'b', 'g', 'k'};
figure();
hold on;
% Placeholder for storing handles
barHandles = gobjects(1, 4);
for ii = 0:3
indices = ii*24 + (1:24);
% Plot and store handles
barHandles(ii+1) = bar(indices, data_kind2(indices), 'FaceColor', colors{ii+1});
end
% Provide handles while setting legend
legend(barHandles, {'Summer', 'Autumn', 'Winter', 'Spring'});
% Set x-tick labels
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'
});
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;
Kindly refer to the documentation of 'bar' and 'legend' for better understanding:
I hope this will help!

Categorie

Scopri di più su Agriculture in Help Center e File Exchange

Prodotti


Release

R2015b

Community Treasure Hunt

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

Start Hunting!

Translated by