Legend position in piecharts
조회 수: 19 (최근 30일)
이전 댓글 표시
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
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
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!
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Data Distribution Plots에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!