📜  Python中的 Matplotlib.patches.Wedge 类

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

Python中的 Matplotlib.patches.Wedge 类

Matplotlib是Python中用于数组二维图的惊人可视化库。 Matplotlib 是一个基于 NumPy 数组构建的多平台数据可视化库,旨在与更广泛的 SciPy 堆栈配合使用。

matplotlib.patches.Wedge

matplotlib.patches.Wedge 类用于在图中添加楔形补丁。楔形以 xy = (x, y) 为中心,半径为 r,它扫过 theta1 到 theta2(以度为单位)。如果给定宽度,则从内半径 r - 宽度到外半径 r 绘制部分楔形。

kwargs 属性如下表所示:

PROPERTYDESCRIPTION
agg_filtera filter function that takes a (m, n, 3) float array and a dpi value that returns a (m, n, 3) array
alphafloat or None
animatedbool
antialiased or aaunknown
capstyle{‘butt’, ’round’, ‘projecting’}
clip_boxBbox
clip_onbool
clip_path[(Path, Transform)|Patch|None]
colorcolor or sequence of rgba tuples
containscallable
edgecolor or ec or edgecolorscolor or None or ‘auto’
facecolor or fc or facecolorscolor or None
figurefigure
fillbool
gidstr
hatch{‘/’, ‘\’, ‘|’, ‘-‘, ‘+’, ‘x’, ‘o’, ‘O’, ‘.’, ‘*’}
in_layoutbool
joinstyle{‘miter’, ’round’, ‘bevel’}
linestyle or ls{‘-‘, ‘–‘, ‘-.’, ‘:’, ”, (offset, on-off-seq), …}
linewidth or linewidths or lwfloat or None
path_effectsAbstractPathEffect
pickerNone or bool or float or callable
path_effectsAbstractPathEffect
pickerfloat or callable[[Artist, Event], Tuple[bool, dict]]
rasterizedbool or None
sketch_params(scale: float, length: float, randomness: float)
snapbool or None
transformmatplotlib.transforms.Transform
urlstr
visiblebool
zorderfloat

示例 1:

Python3
import numpy as np
from matplotlib.patches import Circle, Wedge, Polygon
from matplotlib.collections import PatchCollection
import matplotlib.pyplot as plt
 
 
# Fixing random state for reproducibility
np.random.seed(19680801)
 
fig, ax = plt.subplots()
 
resolution = 50  # the number of vertices
N = 3
x = np.random.rand(N)
y = np.random.rand(N)
radii = 0.1 * np.random.rand(N)
patches = []
 
for x1, y1, r in zip(x, y, radii):
    circle = Circle((x1, y1), r)
    patches.append(circle)
 
x = np.random.rand(N)
y = np.random.rand(N)
radii = 0.1 * np.random.rand(N)
theta1 = 360.0 * np.random.rand(N)
theta2 = 360.0 * np.random.rand(N)
 
for x1, y1, r, t1, t2 in zip(x, y, radii,
                             theta1, theta2):
    wedge = Wedge((x1, y1), r, t1, t2)
    patches.append(wedge)
 
# Some limiting conditions on Wedge
patches += [
    Wedge((.3, .7), .1, 0, 360),             # Full circle
    Wedge((.7, .8), .2, 0, 360, width = 0.05),  # Full ring
    Wedge((.8, .3), .2, 0, 45),              # Full sector
    Wedge((.8, .3), .2, 45, 90, width = 0.10),  # Ring sector
]
 
for i in range(N):
    polygon = Polygon(np.random.rand(N, 2), True)
    patches.append(polygon)
 
colors = 100 * np.random.rand(len(patches))
p = PatchCollection(patches, alpha = 0.4)
p.set_array(np.array(colors))
ax.add_collection(p)
fig.colorbar(p, ax = ax)
 
plt.show()


Python3
import numpy as np
import matplotlib.pyplot as plt
 
 
fig, ax = plt.subplots(figsize =(6, 3),
                       subplot_kw = dict(aspect ="equal"))
 
recipe = ["375 g flour",
          "75 g sugar",
          "250 g butter",
          "300 g berries"]
 
data = [float(x.split()[0]) for x in recipe]
ingredients = [x.split()[-1] for x in recipe]
 
 
def func(pct, allvals):
    absolute = int(pct / 100.*np.sum(allvals))
     
    return "{:.1f}%\n({:d} g)".format(pct, absolute)
 
 
wedges, texts, autotexts = ax.pie(data,
                                  autopct = lambda pct: func(pct, data),
                                  textprops = dict(color ="w"))
 
ax.legend(wedges, ingredients,
          title ="Ingredients",
          loc ="center left",
          bbox_to_anchor =(1, 0, 0.5, 1))
 
plt.setp(autotexts, size = 8, weight ="bold")
 
ax.set_title("Recipe for a pie")
 
plt.show()


输出:

示例 2:

Python3

import numpy as np
import matplotlib.pyplot as plt
 
 
fig, ax = plt.subplots(figsize =(6, 3),
                       subplot_kw = dict(aspect ="equal"))
 
recipe = ["375 g flour",
          "75 g sugar",
          "250 g butter",
          "300 g berries"]
 
data = [float(x.split()[0]) for x in recipe]
ingredients = [x.split()[-1] for x in recipe]
 
 
def func(pct, allvals):
    absolute = int(pct / 100.*np.sum(allvals))
     
    return "{:.1f}%\n({:d} g)".format(pct, absolute)
 
 
wedges, texts, autotexts = ax.pie(data,
                                  autopct = lambda pct: func(pct, data),
                                  textprops = dict(color ="w"))
 
ax.legend(wedges, ingredients,
          title ="Ingredients",
          loc ="center left",
          bbox_to_anchor =(1, 0, 0.5, 1))
 
plt.setp(autotexts, size = 8, weight ="bold")
 
ax.set_title("Recipe for a pie")
 
plt.show()

输出: