Matplotlib.patches.Wedge class in Python

07 Oct, 2021
Matplotlib is an amazing visualization library in Python for 2D plots of arrays. Matplotlib is a multi-platform data visualization library built on NumPy arrays and designed to work with the broader SciPy stack.  
The matplotlib.patches.Wedge class is used to add wedge-shaped patches in the plot. The wedge is centered at xy = (x, y) with a radius r and it sweeps theta1 to theta2 (in degrees). The partial wedge is drawn from inner radius r – width to outer radius r if width is given. 
Syntax: class matplotlib.patches.Wedge(center, r, theta1, theta2, width=None, **kwargs)Parameters:  
center: The center point of the the wedge. r: Radius of the wedge. theta1: First angle of sweep. theta2: Second angle of sweep. width: Width of the sweep 
center: The center point of the the wedge. 
r: Radius of the wedge. 
theta1: First angle of sweep. 
theta2: Second angle of sweep. 
width: Width of the sweep 
The kwargs attributes are given in the table below:  
Example 1:  
Python3
import numpy as npfrom matplotlib.patches import Circle, Wedge, Polygonfrom matplotlib.collections import PatchCollectionimport matplotlib.pyplot as plt  # Fixing random state for reproducibilitynp.random.seed(19680801) fig, ax = plt.subplots() resolution = 50  # the number of verticesN = 3x = 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 Wedgepatches += [    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()
Output:  
Example 2:  
Python3
import numpy as npimport 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()
Output:  
 
adnanirshad158
anikaseth98
Matplotlib patches-class
Python-matplotlib
Python
Write From Home
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.