Saturday, November 16, 2024
Google search engine
HomeLanguagesmatplotlib.axes.Axes.pie() in Python

matplotlib.axes.Axes.pie() in Python

Matplotlib is a library in Python and it is numerical – mathematical extension for NumPy library. The Axes Class contains most of the figure elements: Axis, Tick, Line2D, Text, Polygon, etc., and sets the coordinate system. And the instances of Axes supports callbacks through a callbacks attribute.

matplotlib.axes.Axes.pie() Function

The Axes.pie() function in axes module of matplotlib library is used to plot a pie chart.

Syntax: Axes.pie(self, x, explode=None, labels=None, colors=None, autopct=None, pctdistance=0.6, shadow=False, labeldistance=1.1, startangle=None, radius=None, counterclock=True, wedgeprops=None, textprops=None, center=(0, 0), frame=False, rotatelabels=False, *, data=None) 

Parameters: This method accept the following parameters that are described below:

  • x: This parameter is the wedge sizes.
  • explode: This parameter is the len(x) array which specifies the fraction of the radius with which to offset each wedge.
  • autopct: This parameter is a string or function used to label the wedges with their numeric value.
  • colors: This parameter is the sequence of matplotlib color args through which the pie chart will cycle.
  • label: This parameter is the sequence of strings providing the labels for each wedge.
  • pctdistance: This parameter is the ratio between the center of each pie slice and the start of the text generated by autopct.
  • shadow: This parameter is the used to draw a shadow beneath the pie.
  • labeldistance: This parameter is the radial distance at which the pie labels are drawn.
  • startangle: This parameter is used to rotates the start of the pie chart by angle degrees counterclockwise from the x-axis.
  • radius: This parameter is the radius of the pie.
  • counterclock: This parameter specifies fractions direction, clockwise or counterclockwise.
  • wedgeprops: This parameter is dict of arguments passed to the wedge objects making the pie.
  • textprops: This parameter is dict of arguments to pass to the text objects.
  • center: This parameter is the Center position of the chart.
  • frame: This parameter is used to plot axes frame with the chart if true.
  • rotatelabels: This parameter is used to rotate each label to the angle of the corresponding slice if true.

Returns: This returns the following:

  • patches: This returns the sequence of matplotlib.patches.Wedge instances.
  • texts: This returns the list of the label matplotlib.text.Text instances.
  • autotexts: This returns the list of Text instances for the numeric labels.

Below examples illustrate the matplotlib.axes.Axes.pie() function in matplotlib.axes: 

Example #1: 

Python3




# Implementation of matplotlib function
import matplotlib.pyplot as plt
 
labels = 'Geek1', 'Geek2', 'Geek3', 'Geek4'
sizes = [10, 20, 30, 40]
explode = (0.1, 0, 0, 0)
 
fig1, ax1 = plt.subplots()
ax1.pie(sizes, explode = explode,
        labels = labels, autopct ='% 1.1f %%',
        shadow = True, startangle = 90)
ax1.axis('equal')
 
ax1.set_title('matplotlib.axes.Axes.pie Example')
plt.show()


Output:

  

Example #2: 

Python3




# Implementation of matplotlib function
import matplotlib.pyplot as plt
import numpy as np
 
fig, ax = plt.subplots()
 
size = 0.3
vals = np.array([[90, 43], [57, 60],
                 [92, 20]])
 
cmap = plt.get_cmap("tab20c")
outer_colors = cmap(np.arange(3)*4)
mid_colors = cmap(np.array([1, 2, 3, 4, 5, ]))
inner_colors = cmap(np.array([4, 12, 5,
                              6, 9, 10]))
 
ax.pie(vals.sum(axis = 1), radius = 1,
       colors = outer_colors,
       wedgeprops = dict(width = size,
                         edgecolor ='w'))
 
ax.pie(vals.flatten(), radius = 1-size,
       colors = mid_colors,
       wedgeprops = dict(width = size,
                         edgecolor ='w'))
 
ax.pie(vals.flatten(), radius = 1-2 * size,
       colors = inner_colors,
       wedgeprops = dict(width = size,
                         edgecolor ='w'))
 
ax.set_title('matplotlib.axes.Axes.pie Example')
plt.show()


Output:

RELATED ARTICLES

Most Popular

Recent Comments