How to plot a grouped bar chart?
이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
이전 댓글 표시
0 개 추천
I am trying to stack five comparitive features together for each of the 4 different scenarios along the x axis and the Y axis has year from 2025 to 2050 with increament of 5 years. Each feature should have a different colour and the bar height should be a representative of the cost involved for each feature . How can I do this?
채택된 답변
1 개 추천
댓글 수: 10
Parth
2024년 2월 19일
Thanks a ton.
How should I introduce hatch patterns for each bar in side different scenarios to depict the percentage of work achieved as compared to the targets of 2050? Lets say 0-10 % has a different hatch pattern, 10-20 has a different one and then the code uses the logic to create hatch inside the bars with respect to 2050 goals. Also I will have to add another legend for the percentages.
% Scenario names
x = categorical({'The Hydro-Nucleo Synthesis','The Hydrogen Flourishment','The Technological Gridlock','Nuclear on the Horizon'});
% Features for each scenario
Environmental_Protection = [2035 2038 2045 2033];
Nuclear_Security = [2035 2032 2030 2037];
Safety_And_Energy_Security = [2033 2040 2050 2036];
Fuel_Poverty = [2032 2037 2045 2032];
Waste_Handling_Efficiency = [2036 2031 2038 2039];
% Combine features into a matrix
y = [Environmental_Protection; Nuclear_Security; Safety_And_Energy_Security; Fuel_Poverty; Waste_Handling_Efficiency];
% Plotting
bar(x, y)
% Y-axis limits
ylim([2025 2050])
% Adding legend
legend({'Environmental Protection','Nuclear Security','Safety and Energy Security','Fuel Poverty','Wastes'})
% Adding axis labels
xlabel('Scenarios')
ylabel('Years')
title('Scenario Analysis')

You will need some File Exchange packages for that, in particular hatchfill2 and legendflex. This answer demonstrates their use for rendering hatched bar plots:
Parth
2024년 2월 22일
Thanks . But its giving a few errors!
% Scenario names
x = categorical({'The Hydro-Nucleo Synthesis','The Hydrogen Flourishment','The Technological Gridlock','Nuclear on the Horizon'});
% Features for each scenario
Environmental_Protection = [2035 2038 2045 2033];
Nuclear_Security = [2035 2032 2030 2037];
Safety_And_Energy_Security = [2033 2040 2050 2036];
Fuel_Poverty = [2032 2037 2045 2032];
Waste_Handling_Efficiency = [2036 2031 2038 2039];
% Calculate percentages
total_years = 2050 - 2025 + 1; % Total years from 2025 to 2050
Environmental_Protection_Percentage = (Environmental_Protection - 2025) / total_years * 100;
Nuclear_Security_Percentage = (Nuclear_Security - 2025) / total_years * 100;
Safety_And_Energy_Security_Percentage = (Safety_And_Energy_Security - 2025) / total_years * 100;
Fuel_Poverty_Percentage = (Fuel_Poverty - 2025) / total_years * 100;
Waste_Handling_Efficiency_Percentage = (Waste_Handling_Efficiency - 2025) / total_years * 100;
% Combine percentages into a matrix
y = [Environmental_Protection_Percentage; Nuclear_Security_Percentage; Safety_And_Energy_Security_Percentage; Fuel_Poverty_Percentage; Waste_Handling_Efficiency_Percentage];
% Plotting
bar(x, y)
% Adding hatch patterns
hFig = gcf;
hAx = gca;
for i = 1:numel(hAx.Children)
hatchfill(hAx.Children(i), 'HatchStyle', '/', 'HatchAngle', 45, 'HatchDensity', 20, 'HatchColor', hAx.Children(i).FaceColor);
end
% Adding legend for patterns
legend_patterns = legend({'Pattern Range: 0-20%', 'Pattern Range: 20-40%', 'Pattern Range: 40-60%', 'Pattern Range: 60-80%', 'Pattern Range: 80-100%'});
title(legend_patterns, 'Percentage Range');
% Adding legend for scenarios
legend_scenarios = legend({'Environmental Protection','Nuclear Security','Safety and Energy Security','Fuel Poverty','Wastes'});
title(legend_scenarios, 'Scenarios');
% Adding axis labels
xlabel('Scenarios')
ylabel('Years')
title('Scenario Analysis')
I tried doing this way. Didnt work gave a few errors with function
Matt J
2024년 2월 22일
Thanks . But its giving a few errors!
You don't appear to have posted the errors.
Parth
2024년 2월 23일
Error using hatchfill
Too many input arguments.
Error in Groupedbarchart (line 29)
hatchfill(hAx.Children(i), 'HatchStyle', '/', 'HatchAngle', 45, 'HatchDensity', 20, 'HatchColor', hAx.Children(i).FaceColor);
These are the errors. ANd I am not able to see years on the Y axis but rather percentages.
hatchill2() not hatchfill()

Hb=bar(y');
% Adding legend for patterns
[legend_patterns,hpleg] = legendflex({'Pattern Range: 0-20%', 'Pattern Range: 20-40%', 'Pattern Range: 40-60%',...
'Pattern Range: 60-80%', 'Pattern Range: 80-100%'},'title','Percentage Range',...
'anchor', {'nw','nw'} ,'buffer',[0,-5]);
% Adding legend for scenarios
[legend_scenarios,hsleg] = legendflex({'Environmental Protection','Nuclear Security','Safety and Energy Security',...
'Fuel Poverty','Wastes'},'title','Scenarios','anchor', {'n','n'},'buffer',[-30,-5]);
% Adding axis labels
xlabel('Scenarios')
ylabel('Years')
title('Scenario Analysis')
% Adding hatch patterns
hpleg=findobj(hpleg,'type','Patch');
hsleg=findobj(hsleg,'type','Patch');
for i = 1:numel(Hb)
hatchfill2(Hb(i), 'cross', 'HatchAngle', 45, 'HatchDensity', 20,'HatchColor','k');
hatchfill2(hpleg(i), 'cross', 'HatchAngle', 45, 'HatchDensity', 20,'HatchColor','k');
hatchfill2(hsleg(i), 'cross', 'HatchAngle', 45, 'HatchDensity', 20,'HatchColor','k');
end
xticklabels(x)
Parth
2024년 2월 23일
Hello Sir. Thanks a ton. This is more than perfect. Thanks for the help.
Matt J
2024년 2월 23일
You're welcome, but please Accept-click the answer to indicate that it worked.
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Geodesy and Mapping에 대해 자세히 알아보기
참고 항목
웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 MathWorks 사이트 방문이 최적화되지 않았습니다.
미주
- América Latina (Español)
- Canada (English)
- United States (English)
유럽
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
