Pie chart

Pie chart is a circular representation of component ratios. The angle, and hence the arc length of each sector ratio (also called wedges), presents the proportion that each component accounts for, relative to the whole.

Matplotlib provides the plt.pie() function to draw pie charts. We can label each sector with labels as well as the percentage with autopct automatically. For different ways to customize the string format of the percentages, you may refer to: https://pyformat.info/.
To maintain the circular shape of our pie chart, we specify the same width and length for a square figure with plt.figure(figsize=(n,n)).
Here, we have an example of web server usage in the first week of January 2017:

# Data obtained from https://trends.builtwith.com/web-server on Jan 06, 2017
import matplotlib.pyplot as plt
plt.figure(figsize=(4,4))

x = [0.31,0.3,0.14,0.1,0.15]
labels = ['nginx','Apache','IIS','Varnish','Others']
plt.pie(x,labels=labels,autopct='%1.1f%%')
plt.title('Web Server Usage Statistics')
plt.show()

The resultant pie chart is as follows:

We can also separate each sector by passing a list of ratios to the keyword argument explode. For example, adding the argument explode=[0.1]*5 to the preceding plt.pie() plot will generate the following result:

Please note that if the input array sums up to less than 1, the output pie chart will be incomplete, as shown in the following example:

import matplotlib.pyplot as plt
plt.figure(figsize=(4,4))
x = [0.1,0.3]
plt.pie(x)
plt.show()

As seen here, instead of a full circle, we have an incomplete fan-shaped plot:

In that case, we have to explicitly specify the ratio of each term. For instance, in the preceding example, change x = [0.1,0.3] to x = [0.25,0.75].

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.117.145.11