Documentation

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 themaxfunction to find the largest element inproduct_totalsand return the index of this element,ind.

[c,ind] = max(product_totals);

Use thepiefunction input argument,explode, to offset a pie slice. Theexplodeargument is a vector of zero and nonzero values where the nonzero values indicate the slices to offset. Initializeexplodeas a three-element vector of zeros.

explode = zeros(1,3);

Use the index of the maximum element inproduct_totalsto set the correspondingexplodeelement 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')

See Also

||

Related Topics