Legend does not match with the bar chart in MATLAB

6 ビュー (過去 30 日間)
nazanin tavakoli
nazanin tavakoli 2021 年 6 月 16 日
コメント済み: nazanin tavakoli 2021 年 6 月 16 日
Hello,
I have a bar chart in Matlab. and I have changed 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 is incorrect and does not match with my plot. 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
Could anybody help me? I would really appreciate it if You help me.

採用された回答

Cris LaPierre
Cris LaPierre 2021 年 6 月 16 日
I think the issue is that you are plotting your bars twice. Your legend applies to the data in the order it was plotted, so the first legend entry is applied to the first bar plot, where everything is blue. Then entries 2-4 apply to the first 3 bar plots created in the for loop - red, green and blue.
Since your for loop approach is all you need, remove the first bar command from your code. Try something like this.
% data_kind2=xlsread('kind2.xlsx');
data_kind2 = randi(15,[1,96]);
colors={'r','g','b','k'};
figure();
for ii=0:3
bar(ii*24+(1:24),data_kind2(ii*24+(1:24)),'FaceColor',colors{ii+1})
hold on
end
hold off
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
  1 件のコメント
nazanin tavakoli
nazanin tavakoli 2021 年 6 月 16 日
It seems work. Thank you

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeAgriculture についてさらに検索

製品


リリース

R2015b

Community Treasure Hunt

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

Start Hunting!

Translated by