How to plot a grouped bar chart?

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?

 Accepted Answer

Matt J
Matt J on 19 Feb 2024
Edited: Matt J on 19 Feb 2024

10 Comments

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 on 21 Feb 2024
Edited: Matt J on 22 Feb 2024
You will need some File Exchange packages for that, in particular hatchfill2 and legendflex. This answer demonstrates their use for rendering hatched bar plots:
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
Thanks . But its giving a few errors!
You don't appear to have posted the errors.
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)
Hello Sir. Thanks a ton. This is more than perfect. Thanks for the help.
You're welcome, but please Accept-click the answer to indicate that it worked.

Sign in to comment.

More Answers (0)

Categories

Find more on Geodesy and Mapping in Help Center and File Exchange

Products

Release

R2022b

Asked:

on 19 Feb 2024

Commented:

on 23 Feb 2024

Community Treasure Hunt

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

Start Hunting!