Legend position in piecharts

조회 수: 19 (최근 30일)
Marc
Marc 2023년 11월 4일
댓글: Marc 2023년 11월 21일
Hey guys, I am trying to add a single legend at the bottom of 2 piecharts and also increase the font of the percentages on the chart. See code below.Thanks for your help,
clc
clear
Costs = ["Feedstock","Other Variable Costs","Labour","O & M"];
Cost_Values1 = [13.7341 0.4169 1.0288 2.920 ];
Cost_Values2 = [13.777 1.51 1.226 4.4647 ];
tiledlayout(1,2)
nexttile
p1 = piechart(Cost_Values1,'LegendVisible', 'on', 'FontSize', 10);
%Get the labales
Labels = p1.Labels;
%Change the names of the slices
p1.Names = Costs;
%Changing the names of the slices also changes the labels,
%And that makes things a bit packed/stuffed, so revert back the changes
p1.Labels = Labels;
title("BE-CHP")
nexttile
p2 = piechart(Cost_Values2, 'LegendVisible', 'on', 'FontSize', 10);
Labels = p2.Labels;
p2.Names = Costs;
p2.Labels = Labels;
title("BE-CHPCCS")
colororder meadow;
  댓글 수: 1
Image Analyst
Image Analyst 2023년 11월 5일
Can you specify the Position property of p1? Then just don't set up the labels or names of p2 to get just one "legend".

댓글을 달려면 로그인하십시오.

채택된 답변

Sai Teja G
Sai Teja G 2023년 11월 20일
편집: Sai Teja G 2023년 11월 20일
Hi Marc,
I understand that you wish to enlarge the font size for the percentages and utilize a single legend for both pie charts.
The issue you're experiencing with the font size in the pie charts is due to the size of the legend when using the `piechart()` function, coupled with a horizontal orientation for the `tiledlayout` plots. Since the 'legend' property is linked to the 'piechart()' function, it is not possible to directly set the position of a single legend to be shared by both pie charts simultaneously. To resolve these issues and implement a single legend for both pie charts, please refer to the following code example:
clc
clear
Costs = ["Feedstock","Other Variable Costs","Labour","O & M"];
Cost_Values1 = [13.7341 0.4169 1.0288 2.920 ];
Cost_Values2 = [13.777 1.51 1.226 4.4647 ];
tiledlayout(1,2)
nexttile
p1 = piechart(Cost_Values1,'LegendVisible', 'on', 'FontSize', 10);
%Get the labales
Labels = p1.Labels;
%Change the names of the slices
p1.Names = Costs;
%Changing the names of the slices also changes the labels,
%And that makes things a bit packed/stuffed, so revert back the changes
p1.Labels = Labels;
title("BE-CHP")
nexttile
p2 = piechart(Cost_Values2, 'LegendVisible', 'off', 'FontSize', 10);
Labels = p2.Labels;
p2.Names = Costs;
p2.Labels = Labels;
title("BE-CHPCCS")
colororder meadow;
Hope it helps!
  댓글 수: 1
Marc
Marc 2023년 11월 21일
Thanks man. Another challenge I'm facing is changing the fontweight of the legend and percentages to "bold".

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Data Distribution Plots에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by