Legend position in piecharts

18 views (last 30 days)
Marc
Marc on 4 Nov 2023
Commented: Marc on 21 Nov 2023
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 Comment
Image Analyst
Image Analyst on 5 Nov 2023
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".

Sign in to comment.

Accepted Answer

Sai Teja G
Sai Teja G on 20 Nov 2023
Edited: Sai Teja G on 20 Nov 2023
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 Comment
Marc
Marc on 21 Nov 2023
Thanks man. Another challenge I'm facing is changing the fontweight of the legend and percentages to "bold".

Sign in to comment.

More Answers (0)

Categories

Find more on Data Distribution Plots in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!