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!