Skip to content

ellphi.visualization

Matplotlib helpers for rendering ellipse clouds. For most use cases, EllipseCloud.plot() is the recommended entry point — it calls ellipse_patch internally.

import matplotlib.pyplot as plt
from ellphi.visualization import ellipse_patch

fig, ax = plt.subplots()
patch = ellipse_patch(center, r_major, r_minor, theta, edgecolor="teal", alpha=0.5)
ax.add_patch(patch)
ax.set_aspect("equal")
plt.show()

ellphi.visualization

ellphi.visualization – visualization helpers for ellipse cloud

ellipse_patch(X, r_major=1, r_minor=1, theta=0, *, cov=None, scale=1, **kwgs)

Creates a matplotlib Ellipse patch.

This function can create an ellipse patch from either the major and minor axes and angle, or from a covariance matrix.

Parameters:

Name Type Description Default
X

The center of the ellipse.

required
r_major int

The major radius of the ellipse.

1
r_minor int

The minor radius of the ellipse.

1
theta int

The angle of the major axis in radians.

0
cov

An optional covariance matrix. If provided, r_major, r_minor, and theta will be computed from it.

None
scale int

An optional scaling factor for the ellipse.

1
**kwgs

Additional keyword arguments to pass to the Ellipse patch.

{}

Returns:

Type Description

A matplotlib.patches.Ellipse object.

Examples:

>>> import matplotlib.pyplot as plt
>>> from ellphi.visualization import ellipse_patch
>>> patch = ellipse_patch([0, 0], r_major=2.0, r_minor=1.0, theta=0.0)
>>> type(patch).__name__
'Ellipse'
Source code in src/ellphi/visualization.py
def ellipse_patch(X, r_major=1, r_minor=1, theta=0, *, cov=None, scale=1, **kwgs):
    """Creates a matplotlib `Ellipse` patch.

    This function can create an ellipse patch from either the major and minor
    axes and angle, or from a covariance matrix.

    Args:
        X: The center of the ellipse.
        r_major: The major radius of the ellipse.
        r_minor: The minor radius of the ellipse.
        theta: The angle of the major axis in radians.
        cov: An optional covariance matrix. If provided, `r_major`,
            `r_minor`, and `theta` will be computed from it.
        scale: An optional scaling factor for the ellipse.
        **kwgs: Additional keyword arguments to pass to the `Ellipse` patch.

    Returns:
        A `matplotlib.patches.Ellipse` object.

    Examples:
        >>> import matplotlib.pyplot as plt
        >>> from ellphi.visualization import ellipse_patch
        >>> patch = ellipse_patch([0, 0], r_major=2.0, r_minor=1.0, theta=0.0)
        >>> type(patch).__name__
        'Ellipse'
    """
    if cov is not None:
        r_major, r_minor, theta = axes_from_cov(cov)
    if "facecolor" not in kwgs and "fc" not in kwgs:
        kwgs["facecolor"] = "none"
    ellipse = plt.matplotlib.patches.Ellipse(
        X,
        width=2 * r_major * scale,
        height=2 * r_minor * scale,
        angle=numpy.degrees(theta),
        **kwgs,
    )
    return ellipse