To “explode” a pie chart means to make one of the wedges of the pie chart to stand out. To make this possible in matplotlib
, we use the explode()
parameter.
pyplot(x,explode)
x
: The data array.explode
: It is used to explode the pie chart. If specified, it must be an array with one value representing a value for each wedge of the pie chart.The explode
parameter value represents how far each wedge is from the center of the pie chart.
import matplotlib.pyplot as pltimport numpy as npy = np.array([35, 25, 25, 15])mylabels = ["Tomatoes", "Mangoes", "Oranges", "Apples"]myexplode = [0.2, 0, 0, 0]plt.pie(y, labels = mylabels, explode = myexplode)plt.show()
matplotlib
and modulepyplot
.numpy
library.y
.mylabels
that will represent the label parameter of the pie()
method.myexplode
that contains values which represents the explode
parameter of the pie()
method.pie()
method to create a pie chart with parameter values x
, labels
, and explode
.Note: When we create the
escape
values inmyexplode
variable, the first value is given as0.2
. This represents how far the first wedge (Tomatoes
) is from the center of the pie chart.
Interestingly, we can also increase the distance of other wedges of the pie chart.
import matplotlib.pyplot as pltimport numpy as npy = np.array([35, 25, 25, 15])mylabels = ["Tomatoes", "Mangoes", "Oranges", "Apples"]myexplode = [0.2, 0, 0.4, 0.1]plt.pie(y, labels = mylabels, explode = myexplode)plt.show()
In the code above, we can see that the wedges of the pie chart were given different values, unlike the first code example.