📜  Python中的 matplotlib.axes.Axes.pie()

📅  最后修改于: 2022-05-13 01:54:42.426000             🧑  作者: Mango

Python中的 matplotlib.axes.Axes.pie()

Matplotlib是Python中的一个库,它是 NumPy 库的数值数学扩展。 Axes 类包含大部分图形元素:Axis、Tick、Line2D、Text、Polygon 等,并设置坐标系。 Axes 的实例通过回调属性支持回调。

matplotlib.axes.Axes.pie()函数

matplotlib 库的 axes 模块中的Axes.pie()函数用于绘制饼图。

下面的示例说明了 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()