In this article, we will be going through basic pie to the pie with labels to the pie chart with additional features.
Simple Pie With Labels
To draw pie charts we need the pie function available in pyplot.
#implementing basic pie shape with labels import matplotlib.pyplot as plt import numpy as np y = np.array([35, 25, 25, 15]) mylabels = ["Toys", "Furniture", "Home Decore", "Electronics"] plt.pie(y, labels = mylabels) plt.show()
Output:
Here by default the plotting of the first wedge (one piece) starts from the x-axis and moves counterclockwise.
You can also use startangle=180 as a parameter for the pie to start plotting from 180 degrees.
Now in addition to this basic pie chart, we have some other features like slice labels, auto labeling percentage, explode, and drop-shadow.
Here we will demonstrate the start angle and other features with a pie chart.
#pie chart with additional features/ import matplotlib.pyplot as plt # Pie chart, where the slices will be ordered and plotted counter-clockwise: labels = ["Electronics", "Furniture", "Home Decore", "Toys"] sizes = [35, 25, 25, 15] explode = (0.1, 0, 0, 0) # only "explode" the 2nd slice fig1, ax1 = plt.subplots() ax1.pie(sizes, explode=explode, labels=labels, autopct='%1.1f%%', shadow=True, startangle=90) ax1.axis('equal') # Equal aspect ratio ensures that pie is drawn as a circle. plt.show()
Output:
Here explode is used to show the highest sale percentage for electronics.
also notice that here start angle is 90.
Hope it is helpful to you. For suggestions leave a comment.
Thanks for read.