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?

 채택된 답변

Matt J
Matt J 2024년 2월 19일
편집: Matt J 2024년 2월 19일

1 개 추천

댓글 수: 10

Parth
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')
Matt J
Matt J 2024년 2월 21일
편집: Matt J 2024년 2월 22일
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
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
Matt J 2024년 2월 22일
Thanks . But its giving a few errors!
You don't appear to have posted the errors.
Parth
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.
Matt J
Matt J 2024년 2월 23일
편집: Matt J 2024년 2월 23일
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
Parth 2024년 2월 23일
Hello Sir. Thanks a ton. This is more than perfect. Thanks for the help.
Matt J
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에 대해 자세히 알아보기

제품

릴리스

R2022b

질문:

2024년 2월 19일

댓글:

2024년 2월 23일

Community Treasure Hunt

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

Start Hunting!

Translated by