PLOT A PIE CHART IN MATPLOTLIB
Listing 5.21 displays the content of piechart1.py that illustrates how to plot a pie chart in Matplotlib.
LISTING 5.21: piechart1.py
import numpy as np
# data to display on plots
x = [1, 2, 3, 4]
# explode the first wedge:
e =(0.1, 0, 0, 0)
plt.pie(x, explode = e)
plt.title("Pie chart")
plt.show()
Listing 5.21 contains an import statement followed by the variables x and e that are initialized as a list of numbers. The values for x are used to calculate the relative size of each “slice” of the pie chart, and the values for the variable e indicate that the first pie slice is “exploded” slightly (indicated by the value 0.1 in e). Launch the code in Listing 5.21, and you will see the pie chart displayed in Figure 5.18.

FIGURE 5.18 A pie chart generated by the code in Listing 5.21