Pie Chart

Pie Chart


  Data visualization

Contents

  • Pie Chart

Pie Chart

A pie chart is a circular chart divided into segments. The segments of a pie chart are called wedges. Each wedge represents an individual category. Pie charts display the contribution of each wedge to the total value and the sum of the values of all wedges should be equal to the whole circle. Pie charts are useful when there are few categories to compare(5 or less) else it becomes difficult to interpret the data.

Below we see a pie chart plotted using the pie function defined in pyplot module. The pie chart displays the percentage of marks obtained by a student in four subjects. The circle is divided into four wedges and the area of each wedge is proportionate to the value it represents.

from matplotlib import pyplot as plt
plt.style.use('ggplot')
x = [22,18,13,10]
labels = ['maths','physics','chemistry','english']
colors = ['m','r','y','c']
plt.pie(x,labels=labels,colors=colors,autopct='%.1f%%')
plt.title('Marks obtained in an exam')
plt.show()

The pie chart below displays the market share of mobile phone vendors worldwide.

plt.style.use('ggplot')
x = [33.71,19.37,4.85,3.82,7.42,30.83]
labels = ['Samsung','Apple','Huawei','LG','Unknown','Others']
colors = ['m','r','y','b','g','c']    
plt.pie(x,labels=labels,colors=colors, explode=[0,0,0,.2,0,0],autopct='%1.2f%%',startangle=45,counterclock =True,shadow=True,
        wedgeprops={"edgecolor":"k",'linewidth': 1,'linestyle': '-'})
plt.title ('Smartphone market share')
plt.axis('equal')
plt.legend(loc=2)
plt.show()

x — The first argument passed to the pie function is an array or a list denoting the values for the categories to be compared.

Labels — The labels argument is a list of strings used for labelling each wedge.

Colors — You can define an array/list of colors and then pass it to the pie function that will be applied to each wedge in pie chart in the order specified in the array.

Explode — If you want to highlight or emphasize key data in a pie chart use the explode parameter. The explode parameter explodes/expands a wedge, so the wedge is moved slightly outward from its center. This parameter accepts an array and each element in the array specifies by what fraction of the radius the wedge needs to be exploded. The value has to be defined for all wedges in the pie chart, so the length of the array should be equal to the number of wedges in the pie chart.

Autopct — If you want to label the wedges with their numeric value in a pie chart, then use autopct parameter. This parameter allows us to display the percent value using string formatting. Say for example, the percent value calculated for a segment is 34.678666 and if you want to display the percent value rounded to 1 decimal place then autopct parameter should be assigned the format string ‘%1.1f’ then the wedge will be labelled with the numeric value 34.6. If you want to add a percent sign (%) to the label then use two percent signs(%%) in the format string so that the special character ‘%’ escapes itself.

Startangle — By default the Startangle is zero, which means starting from the positive x-axis the wedges are arranged in the counter clock wise direction. If you specify a different Startangle then the start of the pie chart is shifted by this angle in degrees and then the wedges are arranged in counter clock wise direction from this position.

Counterclock — Specifies the direction in which the wedges are arranged, clockwise or counter clockwise. The default value is True.

Shadow — A shadow effect can be added to the pie chart using the shadow parameter of the pie() function, passing boolean value – True will make a shadow appear below the pie chart. By default shadow is turned off.

Wedgeprops — The wedges of the pie chart can be customized using the wedgeprop parameter. A dictionary with the property name and value as the key, value pairs can be passed as the wedgeprop argument. The wedge properties like edgecolor, linestyle, linewidth can be specified.

%d bloggers like this: