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?
採用された回答
10 件のコメント
Parth
2024 年 2 月 19 日
Thanks a ton.
Parth
2024 年 2 月 21 日
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!
Parth
2024 年 2 月 22 日
% 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 で Labels and Annotations についてさらに検索
参考
Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の 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)
