Multivariate Normal Distribution Visualizations

PythonPythonBeginner
Practice Now

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

Introduction

This lab explores various normalizations on a multivariate normal distribution using Python Matplotlib. In this lab, you will learn about linear normalization, power law normalization, and how to use Matplotlib to visualize the multivariate normal distribution.

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.

Import Libraries

In this step, you need to import the necessary libraries which are Matplotlib, NumPy, and Multivariate_normal from NumPy.random.

import matplotlib.pyplot as plt
import numpy as np
from numpy.random import multivariate_normal

Set Random State

In this step, you need to set the random state for reproducibility.

np.random.seed(19680801)

Create Data

In this step, you need to create data using multivariate_normal(). This function generates a random sample from a multivariate normal distribution.

data = np.vstack([
    multivariate_normal([10, 10], [[3, 2], [2, 3]], size=100000),
    multivariate_normal([30, 20], [[3, 1], [1, 3]], size=1000)
])

Create Histogram

In this step, you need to create a histogram using hist2d(). The hist2d() function is used to create a two-dimensional histogram.

plt.hist2d(data[:, 0], data[:, 1], bins=100)

Create Power Law Normalization

In this step, you need to create power law normalization using PowerNorm().

plt.hist2d(data[:, 0], data[:, 1], bins=100, norm=mcolors.PowerNorm(gamma))

Create Subplots

In this step, you need to create subplots using subplots().

fig, axs = plt.subplots(nrows=2, ncols=2)

Create Linear Normalization

In this step, you need to create linear normalization.

axs[0, 0].hist2d(data[:, 0], data[:, 1], bins=100)

Create Power Law Normalization

In this step, you need to create power law normalization with different gamma values.

for ax, gamma in zip(axs.flat[1:], gammas):
    ax.hist2d(data[:, 0], data[:, 1], bins=100, norm=mcolors.PowerNorm(gamma))

Set Title

In this step, you need to set the title of each plot.

axs[0, 0].set_title('Linear normalization')

for ax, gamma in zip(axs.flat[1:], gammas):
    ax.set_title(r'Power law $(\gamma=%1.1f)$' % gamma)

Tight Layout

In this step, you need to adjust the spacing between subplots.

fig.tight_layout()

Show Plot

In this step, you need to display the plot using show().

plt.show()

Summary

This lab explored various normalizations on a multivariate normal distribution using Python Matplotlib. You learned about linear normalization, power law normalization, and how to use Matplotlib to visualize the multivariate normal distribution.

Other Python Tutorials you may like