Plotting Confidence Ellipses With Matplotlib

PythonPythonBeginner
Practice Now

This tutorial is from open-source community. Access the source code

Introduction

This lab will demonstrate how to plot confidence ellipses of a two-dimensional dataset using Python Matplotlib. A confidence ellipse is a graphical representation of the covariance of a dataset, showing the uncertainty of the estimated mean and standard deviation. The ellipses are plotted using the Pearson correlation coefficient.

VM Tips

After the VM startup is done, click the top left corner to switch to the Notebook tab to access Jupyter Notebook for practice.

Sometimes, you may need to wait a few seconds for Jupyter Notebook to finish loading. The validation of operations cannot be automated because of limitations in Jupyter Notebook.

If you face issues during learning, feel free to ask Labby. Provide feedback after the session, and we will promptly resolve the problem for you.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL python(("`Python`")) -.-> python/BasicConceptsGroup(["`Basic Concepts`"]) python(("`Python`")) -.-> python/FunctionsGroup(["`Functions`"]) python(("`Python`")) -.-> python/FileHandlingGroup(["`File Handling`"]) matplotlib(("`Matplotlib`")) -.-> matplotlib/BasicConceptsGroup(["`Basic Concepts`"]) matplotlib(("`Matplotlib`")) -.-> matplotlib/PlottingDataGroup(["`Plotting Data`"]) matplotlib(("`Matplotlib`")) -.-> matplotlib/PlotCustomizationGroup(["`Plot Customization`"]) python(("`Python`")) -.-> python/ControlFlowGroup(["`Control Flow`"]) python(("`Python`")) -.-> python/DataStructuresGroup(["`Data Structures`"]) python(("`Python`")) -.-> python/ModulesandPackagesGroup(["`Modules and Packages`"]) python(("`Python`")) -.-> python/ErrorandExceptionHandlingGroup(["`Error and Exception Handling`"]) python(("`Python`")) -.-> python/PythonStandardLibraryGroup(["`Python Standard Library`"]) python(("`Python`")) -.-> python/DataScienceandMachineLearningGroup(["`Data Science and Machine Learning`"]) python/BasicConceptsGroup -.-> python/comments("`Comments`") python/FunctionsGroup -.-> python/keyword_arguments("`Keyword Arguments`") python/FileHandlingGroup -.-> python/with_statement("`Using with Statement`") matplotlib/BasicConceptsGroup -.-> matplotlib/importing_matplotlib("`Importing Matplotlib`") matplotlib/BasicConceptsGroup -.-> matplotlib/figures_axes("`Understanding Figures and Axes`") matplotlib/PlottingDataGroup -.-> matplotlib/scatter_plots("`Scatter Plots`") matplotlib/PlotCustomizationGroup -.-> matplotlib/line_styles_colors("`Customizing Line Styles and Colors`") matplotlib/PlotCustomizationGroup -.-> matplotlib/legend_config("`Legend Configuration`") python/BasicConceptsGroup -.-> python/variables_data_types("`Variables and Data Types`") python/BasicConceptsGroup -.-> python/numeric_types("`Numeric Types`") python/ControlFlowGroup -.-> python/conditional_statements("`Conditional Statements`") python/ControlFlowGroup -.-> python/for_loops("`For Loops`") python/DataStructuresGroup -.-> python/lists("`Lists`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/DataStructuresGroup -.-> python/dictionaries("`Dictionaries`") python/FunctionsGroup -.-> python/function_definition("`Function Definition`") python/FunctionsGroup -.-> python/default_arguments("`Default Arguments`") python/ModulesandPackagesGroup -.-> python/importing_modules("`Importing Modules`") python/ModulesandPackagesGroup -.-> python/standard_libraries("`Common Standard Libraries`") python/ErrorandExceptionHandlingGroup -.-> python/raising_exceptions("`Raising Exceptions`") python/PythonStandardLibraryGroup -.-> python/math_random("`Math and Random`") python/DataScienceandMachineLearningGroup -.-> python/numerical_computing("`Numerical Computing`") python/DataScienceandMachineLearningGroup -.-> python/data_visualization("`Data Visualization`") python/FunctionsGroup -.-> python/build_in_functions("`Build-in Functions`") subgraph Lab Skills python/comments -.-> lab-48615{{"`Plotting Confidence Ellipses With Matplotlib`"}} python/keyword_arguments -.-> lab-48615{{"`Plotting Confidence Ellipses With Matplotlib`"}} python/with_statement -.-> lab-48615{{"`Plotting Confidence Ellipses With Matplotlib`"}} matplotlib/importing_matplotlib -.-> lab-48615{{"`Plotting Confidence Ellipses With Matplotlib`"}} matplotlib/figures_axes -.-> lab-48615{{"`Plotting Confidence Ellipses With Matplotlib`"}} matplotlib/scatter_plots -.-> lab-48615{{"`Plotting Confidence Ellipses With Matplotlib`"}} matplotlib/line_styles_colors -.-> lab-48615{{"`Plotting Confidence Ellipses With Matplotlib`"}} matplotlib/legend_config -.-> lab-48615{{"`Plotting Confidence Ellipses With Matplotlib`"}} python/variables_data_types -.-> lab-48615{{"`Plotting Confidence Ellipses With Matplotlib`"}} python/numeric_types -.-> lab-48615{{"`Plotting Confidence Ellipses With Matplotlib`"}} python/conditional_statements -.-> lab-48615{{"`Plotting Confidence Ellipses With Matplotlib`"}} python/for_loops -.-> lab-48615{{"`Plotting Confidence Ellipses With Matplotlib`"}} python/lists -.-> lab-48615{{"`Plotting Confidence Ellipses With Matplotlib`"}} python/tuples -.-> lab-48615{{"`Plotting Confidence Ellipses With Matplotlib`"}} python/dictionaries -.-> lab-48615{{"`Plotting Confidence Ellipses With Matplotlib`"}} python/function_definition -.-> lab-48615{{"`Plotting Confidence Ellipses With Matplotlib`"}} python/default_arguments -.-> lab-48615{{"`Plotting Confidence Ellipses With Matplotlib`"}} python/importing_modules -.-> lab-48615{{"`Plotting Confidence Ellipses With Matplotlib`"}} python/standard_libraries -.-> lab-48615{{"`Plotting Confidence Ellipses With Matplotlib`"}} python/raising_exceptions -.-> lab-48615{{"`Plotting Confidence Ellipses With Matplotlib`"}} python/math_random -.-> lab-48615{{"`Plotting Confidence Ellipses With Matplotlib`"}} python/numerical_computing -.-> lab-48615{{"`Plotting Confidence Ellipses With Matplotlib`"}} python/data_visualization -.-> lab-48615{{"`Plotting Confidence Ellipses With Matplotlib`"}} python/build_in_functions -.-> lab-48615{{"`Plotting Confidence Ellipses With Matplotlib`"}} end

Import Required Libraries

The first step is to import the necessary libraries. We will need numpy and matplotlib.pyplot for this lab.

import matplotlib.pyplot as plt
import numpy as np

Define the confidence_ellipse Function

Next, we define the confidence_ellipse function that will take in the x and y coordinates of the dataset, the axes object to draw the ellipse into, and the number of standard deviations. It returns a Matplotlib patch object representing the ellipse.

def confidence_ellipse(x, y, ax, n_std=3.0, facecolor='none', **kwargs):
    """
    Create a plot of the covariance confidence ellipse of *x* and *y*.

    Parameters
    ----------
    x, y : array-like, shape (n, )
        Input data.

    ax : matplotlib.axes.Axes
        The axes object to draw the ellipse into.

    n_std : float
        The number of standard deviations to determine the ellipse's radiuses.

    **kwargs
        Forwarded to `~matplotlib.patches.Ellipse`

    Returns
    -------
    matplotlib.patches.Ellipse
    """
    if x.size != y.size:
        raise ValueError("x and y must be the same size")

    cov = np.cov(x, y)
    pearson = cov[0, 1]/np.sqrt(cov[0, 0] * cov[1, 1])
    ## Using a special case to obtain the eigenvalues of this
    ## two-dimensional dataset.
    ell_radius_x = np.sqrt(1 + pearson)
    ell_radius_y = np.sqrt(1 - pearson)
    ellipse = Ellipse((0, 0), width=ell_radius_x * 2, height=ell_radius_y * 2,
                      facecolor=facecolor, **kwargs)

    ## Calculating the standard deviation of x from
    ## the squareroot of the variance and multiplying
    ## with the given number of standard deviations.
    scale_x = np.sqrt(cov[0, 0]) * n_std
    mean_x = np.mean(x)

    ## calculating the standard deviation of y ...
    scale_y = np.sqrt(cov[1, 1]) * n_std
    mean_y = np.mean(y)

    transf = transforms.Affine2D() \
        .rotate_deg(45) \
        .scale(scale_x, scale_y) \
        .translate(mean_x, mean_y)

    ellipse.set_transform(transf + ax.transData)
    return ax.add_patch(ellipse)

Define the get_correlated_dataset Function

We also need a function to generate a two-dimensional dataset with specified mean, dimensions, and correlation.

def get_correlated_dataset(n, dependency, mu, scale):
    """
    Creates a random two-dimensional dataset with the specified
    two-dimensional mean (mu) and dimensions (scale).
    The correlation can be controlled by the param 'dependency',
    a 2x2 matrix.
    """
    latent = np.random.randn(n, 2)
    dependent = latent.dot(dependency)
    scaled = dependent * scale
    scaled_with_offset = scaled + mu
    ## return x and y of the new, correlated dataset
    return scaled_with_offset[:, 0], scaled_with_offset[:, 1]

Plotting Positive, Negative, and Weak Correlations

Now, we can use these functions to plot the confidence ellipses of datasets with positive, negative, and weak correlations.

np.random.seed(0)

PARAMETERS = {
    'Positive correlation': [[0.85, 0.35],
                             [0.15, -0.65]],
    'Negative correlation': [[0.9, -0.4],
                             [0.1, -0.6]],
    'Weak correlation': [[1, 0],
                         [0, 1]],
}

mu = 2, 4
scale = 3, 5

fig, axs = plt.subplots(1, 3, figsize=(9, 3))
for ax, (title, dependency) in zip(axs, PARAMETERS.items()):
    x, y = get_correlated_dataset(800, dependency, mu, scale)
    ax.scatter(x, y, s=0.5)

    ax.axvline(c='grey', lw=1)
    ax.axhline(c='grey', lw=1)

    confidence_ellipse(x, y, ax, edgecolor='red')

    ax.scatter(mu[0], mu[1], c='red', s=3)
    ax.set_title(title)

plt.show()

Plotting Different Number of Standard Deviations

We can also plot the confidence ellipses with different number of standard deviations.

fig, ax_nstd = plt.subplots(figsize=(6, 6))

dependency_nstd = [[0.8, 0.75],
                   [-0.2, 0.35]]
mu = 0, 0
scale = 8, 5

ax_nstd.axvline(c='grey', lw=1)
ax_nstd.axhline(c='grey', lw=1)

x, y = get_correlated_dataset(500, dependency_nstd, mu, scale)
ax_nstd.scatter(x, y, s=0.5)

confidence_ellipse(x, y, ax_nstd, n_std=1,
                   label=r'$1\sigma$', edgecolor='firebrick')
confidence_ellipse(x, y, ax_nstd, n_std=2,
                   label=r'$2\sigma$', edgecolor='fuchsia', linestyle='--')
confidence_ellipse(x, y, ax_nstd, n_std=3,
                   label=r'$3\sigma$', edgecolor='blue', linestyle=':')

ax_nstd.scatter(mu[0], mu[1], c='red', s=3)
ax_nstd.set_title('Different standard deviations')
ax_nstd.legend()
plt.show()

Using Keyword Arguments

Lastly, we can customize the appearance of the ellipses using keyword arguments.

fig, ax_kwargs = plt.subplots(figsize=(6, 6))
dependency_kwargs = [[-0.8, 0.5],
                     [-0.2, 0.5]]
mu = 2, -3
scale = 6, 5

ax_kwargs.axvline(c='grey', lw=1)
ax_kwargs.axhline(c='grey', lw=1)

x, y = get_correlated_dataset(500, dependency_kwargs, mu, scale)
## Plot the ellipse with zorder=0 in order to demonstrate
## its transparency (caused by the use of alpha).
confidence_ellipse(x, y, ax_kwargs,
                   alpha=0.5, facecolor='pink', edgecolor='purple', zorder=0)

ax_kwargs.scatter(x, y, s=0.5)
ax_kwargs.scatter(mu[0], mu[1], c='red', s=3)
ax_kwargs.set_title('Using keyword arguments')

fig.subplots_adjust(hspace=0.25)
plt.show()

Summary

In this lab, we learned how to plot confidence ellipses of a two-dimensional dataset using Python Matplotlib. We defined the confidence_ellipse and get_correlated_dataset functions, and used them to plot ellipses of datasets with different correlations and number of standard deviations. We also showed how to customize the appearance of the ellipses using keyword arguments.

Other Python Tutorials you may like