Offset Pie Slice with Greatest Contribution
This example shows how to create a pie graph and automatically offset the pie slice with the greatest contribution.
Set up a three-column array,X
, so that each column contains yearly sales data for a specific product over a 5-year period.
X = [19.3, 22.1, 51.6 34.2, 70.3, 82.4 61.4, 82.9, 90.8 50.5, 54.9, 59.1 29.4, 36.3, 47.0];
Calculate the total sales for each product over the 5-year period by taking the sum of each column. Store the results inproduct_totals
.
product_totals = sum(X);
Use themax
function to find the largest element inproduct_totals
and return the index of this element,ind
.
[c,ind] = max(product_totals);
Use thepie
function input argument,explode
, to offset a pie slice. Theexplode
argument is a vector of zero and nonzero values where the nonzero values indicate the slices to offset. Initializeexplode
as a three-element vector of zeros.
explode = zeros(1,3);
Use the index of the maximum element inproduct_totals
to set the correspondingexplode
element to 1.
explode(ind) = 1;
Create a pie chart of the sales totals for each product and offset the pie slice for the product with the largest total sales.
figure pie(product_totals,explode) title('Sales Contributions of Three Products')