Matplotlib - Mmh Donuts



A donut is a circular pastry with a hole in the center, giving it a classic ring shape. The outer surface of donut is usually smooth and glazed, adding a shiny finish. The shape defines a perfect balance of curvature.

Mmh Donut

Mmh Donuts in Matplotlib

In Matplotlib, we can create a donut shape by drawing circular wedges for both the outer and inner circles. We can use the Wedge class in the "matplotlib.patches" module to define these circles and combine them to create a donut appearance.

You can customize the donut by adjusting the center, inner and outer radii, and colors, and remember to set equal aspect ratios for a circular look.

The matplotlib.patches.Wedge Class

The matplotlib.patches.Wedge class in Matplotlib is used to make wedge-shaped patches, which are like circular sectors. These wedges are created by specifying their center coordinates, inner and outer radii, and angular extent. By instantiating objects of this class, you can easily add these wedge shapes to your Matplotlib plot.

Following is the syntax for creating a donut using the Wedge class in Matplotlib −

matplotlib.patches.Wedge(center, r, theta1, theta2, width=None, **kwargs)

Where,

  • centeris the tuple (x, y) representing the center coordinates of the wedge.
  • r is the outer radius of the wedge.
  • theta1is the starting angle of the wedge in degrees.
  • theta2 is the ending angle of the wedge in degrees.
  • width (optional) is the width of the wedge (default is None).
  • **kwargs is the additional keyword arguments for customizing the wedge appearance.

Let us start by creating a basic donut.

Basic Donut

You can create a basic donut shape in Matplotlib using the matplotlib.patches.Wedge class. The donut is defined by specifying its center coordinates, outer and inner radii, and the width of the ring. The resulting plot displays a circular ring with a different face color and edge color, representing a basic donut.

Example

In the following example, we are creating a basic donut shape using the matplotlib.patches.Wedge class. We are first defining the outer and inner radii as "0.8" and "0.4" respectively. We are then passing the arguments such as center coordinates, outer radius, inner radius, and width to the patches.wedge() function −

import matplotlib.pyplot as plt
import matplotlib.patches as patches

# Creating a figure and axis
fig, ax = plt.subplots()

# Defining the outer and inner radii
outer_radius = 0.8
inner_radius = 0.4

# Creating a Wedge representing the donut
donut = patches.Wedge((0, 0), outer_radius, 0, 360, width=outer_radius - inner_radius, facecolor='brown', edgecolor='black')

# Adding the donut to the plot
ax.add_patch(donut)

# Setting equal aspect ratio for a circular look
ax.set_aspect('equal', adjustable='box')

# Display the plot
plt.title('Basic Donut')
# Adjust the x-axis limit
plt.xlim(-1, 1)
# Adjust the y-axis limit
plt.ylim(-1, 1)  
plt.show()

Output

After executing the above code, we get the following output −

Basic Donut

Double Donut

You can create a double donut as well in matplotlib by drawing two concentric circles. To achieve this, define each donut by specifying the center coordinates, outer and inner radii, and width. The resulting plot will displays two distinct donuts with different face colors and edges.

Example

In here, we are creating two concentric donuts in the same plot. We are defining each donut by its unique outer and inner radii, and applying distinct face colors and edge colors −

import matplotlib.pyplot as plt
import matplotlib.patches as patches

# Creating a figure and axis
fig, ax = plt.subplots()

# Defining the radii for the outer and inner circles of the first donut
outer_radius_1 = 0.8
inner_radius_1 = 0.4

# Creating a Wedge representing the first donut
donut1 = patches.Wedge((0, 0), outer_radius_1, 0, 360, width=outer_radius_1 - inner_radius_1, facecolor='brown', edgecolor='black')

# Defining the radii for the outer and inner circles of the second donut
outer_radius_2 = 0.6
inner_radius_2 = 0.2

# Creating a Wedge representing the second donut
donut2 = patches.Wedge((0, 0), outer_radius_2, 0, 360, width=outer_radius_2 - inner_radius_2, facecolor='lightgreen', edgecolor='black')

# Adding the donuts to the plot
ax.add_patch(donut1)
ax.add_patch(donut2)

# Setting equal aspect ratio for a circular look
ax.set_aspect('equal', adjustable='box')

# Displaying the plot
plt.title('Double Donut')
plt.xlim(-1, 1)  
plt.ylim(-1, 1)  
plt.show()

Output

Following is the output of the above code −

Double Donut

Patterned Donut

You can also create a patterned donut with a textured appearance using the matplotlib.patches.Wedge class. This class allows you to specify the center coordinates, outer and inner radii, as well as the width of the donut. To enhance its visual appeal, fill the donut's face with a crosshatch pattern ('xx') using the hatch parameter.

The resulting plot displays a donut with a distinct style, combining the circular shape with a specific hatch pattern.

Example

Now, we are creating a donut with a light yellow face color, a black edge, and 'xx' hatch patterns applied to its face −

import matplotlib.pyplot as plt
import matplotlib.patches as patches
fig, ax = plt.subplots()
outer_radius = 0.8
inner_radius = 0.4

# Creating a Wedge representing the donut
donut = patches.Wedge((0, 0), outer_radius, 0, 360, width=outer_radius - inner_radius, edgecolor='black', hatch='xx', facecolor='yellow')
ax.add_patch(donut)
ax.set_aspect('equal', adjustable='box')
plt.title('Patterned Donut')
plt.xlim(-1, 1)  
plt.ylim(-1, 1)  
plt.show()

Output

Output of the above code is as follows −

Patterned Donut

Custom Donut

We can create a unique donut shape by customizing it using the matplotlib.patches.Wedge class. We can provide distinct angular extent to the donut, forming a partial circle. To enhance the shape visually, we can style the donut's edge with a dashed line ('--').

Example

In the example below, we are customizing the donut with a specific angular extent, creating a partial circle. We are styling the donut with a light blue face color, a dark blue edge, and a dashed linestyle as shown below −

import matplotlib.pyplot as plt
import matplotlib.patches as patches
fig, ax = plt.subplots()
outer_radius = 0.8
inner_radius = 0.4

# Creating a Wedge representing the custom donut with a specific angular extent
donut = patches.Wedge((0, 0), outer_radius, 45, 315, width=outer_radius - inner_radius, edgecolor='darkblue', linestyle='--', facecolor='lightblue')

# Adding the donut to the plot
ax.add_patch(donut)
ax.set_aspect('equal', adjustable='box')
plt.title('Custom Donut')
plt.xlim(-1, 1)  
plt.ylim(-1, 1)  
plt.show()

Output

The output obtained is as shown below −

Custom Donut
Advertisements