Python中的 matplotlib.axes.Axes.pie()
Matplotlib是Python中的一个库,它是 NumPy 库的数值数学扩展。 Axes 类包含大部分图形元素:Axis、Tick、Line2D、Text、Polygon 等,并设置坐标系。 Axes 的实例通过回调属性支持回调。
matplotlib.axes.Axes.pie()函数
matplotlib 库的 axes 模块中的Axes.pie()函数用于绘制饼图。
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 is a 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.
下面的示例说明了 matplotlib.axes 中的 matplotlib.axes.Axes.pie()函数:
示例 #1:
# 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()
输出:
示例 #2:
# 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()