Matplotlib - Ellipse Demo



An ellipse is a geometric shape that looks like a slightly stretched circle. It has a curved outline, and unlike a circle, it has two different diameters; a longer one called the major axis and a shorter one called the minor axis. The center of the ellipse is a point around which the shape is symmetrically balanced.

Ellipse Demo

Ellipse Demo in Matplotlib

We can create an ellipse demo in Matplotlib using the "Ellipse" class in the "matplotlib.patches" module. The Ellipse class allows you to define the center, width and height (major and minor axes), angle of rotation, and other properties of the ellipse.

The matplotlib.patches.Ellipse Class

The matplotlib.patches.Ellipse class constructs an elliptical patch in Matplotlib plots. It takes parameters for the center coordinates, width (major axis length), height (minor axis length), and angle (rotation angle in degrees) and returns an ellipse instance.

Following is the syntax for creating an ellipse using the Ellipse class in Matplotlib −

patches.Ellipse(xy, width, height, angle=0.0, **kwargs)

Where,

  • xy is the center of the ellipse as a tuple (x, y).
  • width is the width of the ellipse.
  • height is the height of the ellipse.
  • angle (optional) is the rotation angle of the ellipse in degrees (default is 0.0).
  • **kwargs is the additional keyword arguments for customization.

Let us start by creating a rotated ellipse.

Rotated Ellipse

We can create a rotated ellipse in Matplotlib using the Ellipse class from the matplotlib.patches module. The rotation angle allows the ellipse to be tilted or rotated around its center. This can be achieved by specifying the angle parameter when creating the Ellipse object.

Example

In the following example, we are creating a rotated ellipse with a 45-degree angle. The ellipse has a width of 3 units, a height of 2 units, an edgecolor of blue ('b'), and no face color −

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

fig, ax = plt.subplots()

# Rotated ellipse
ellipse = patches.Ellipse((0, 0), 3, 2, angle=45, edgecolor='b', facecolor='none')
ax.add_patch(ellipse)

plt.title('Rotated Ellipse')
# Setting axis to be equal 
plt.axis('equal')  
plt.show()

Output

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

Rotated Ellipse

Ellipse with Custom Color

We can customize the color of an ellipse as well in matplotlib. We achieve this by specifying the desired edge and face colors when creating the Ellipse object. The "edgecolor" parameter determines the color of the ellipse's outline, while the "facecolor" parameter sets the color of its interior.

Example

In here, we are creating an ellipse having a green ('g') edge color and a yellow face color, providing a colorful appearance −

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

fig, ax = plt.subplots()

# Ellipse with custom colors
ellipse = patches.Ellipse((0, 0), 3, 2, edgecolor='g', facecolor='yellow')
ax.add_patch(ellipse)

plt.title('Ellipse with Custom Colors')
# Setting axis to be equal 
plt.axis('equal') 
plt.show()

Output

Following is the output of the above code −

Ellipse with Custom Color

Multiple Ellipses

We can also add multiple ellipses to a single plot by creating multiple instances of the Ellipse class. This allows us to represent various shapes simultaneously, each with its own unique characteristics and positions.

Example

Now, we are creating two ellipses "ellipse1" and "ellipse2" with different sizes and colors, and adding them to the axes −

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

fig, ax = plt.subplots()

# Multiple ellipses
ellipse1 = patches.Ellipse((0, 0), 3, 2, edgecolor='r', facecolor='none')
ellipse2 = patches.Ellipse((0, 0), 2, 4, edgecolor='b', facecolor='none')

ax.add_patch(ellipse1)
ax.add_patch(ellipse2)

plt.title('Multiple Ellipses')
# Setting axis to be equal 
plt.axis('equal')
plt.show()

Output

Output of the above code is as follows −

Multiple Ellipses

Ellipse with Transparency

You can customize the transparency, often referred to as alpha blending, of an ellipse, by setting the alpha parameter when creating the Ellipse object. This parameter controls the opacity of the ellipse, allowing us to create semi-transparent shapes that overlay with other elements in the plot.

Example

In the example below, we are creating an ellipse with a pink face color and 10% transparency (alpha=0.1) −

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

fig, ax = plt.subplots()

# Ellipse with transparency
ellipse = patches.Ellipse((0, 0), 3, 2, edgecolor='r', facecolor='pink', alpha=0.1)
ax.add_patch(ellipse)

plt.title('Ellipse with Transparency')
# Setting axis to be equal 
plt.axis('equal')
plt.show()

Output

The output obtained is as shown below −

Ellipse with Transparency
Advertisements