# Contour Plots¶

• toc:true
• author: Sanyam Kapoor
• categories: [contourplot, colorbar]
In [1]:
#collapse-hide

import numpy as np
from scipy.stats import multivariate_normal as mvn

def mog2(x):
L  = np.array([[1., 0], [1., 2.]])
return mvn(mean=np.array([-2., 2.]),
cov=np.matmul(L, L.T)).pdf(x) + \
mvn(mean=np.array([2., -2.])).pdf(x)

## Density for a Mixture of Two Gaussians.
x = np.mgrid[-5:5:.1, -5:6.5:.1]
pdf = mog2(x.reshape(2, -1).T).reshape(*x.shape[1:])

In [2]:
#collapse-hide

import matplotlib.pyplot as plt
import seaborn as sns

sns.set(font_scale=2., style='whitegrid')


## Contour Lines Plot¶

In [3]:
fig, ax = plt.subplots(figsize=(7,7))

ax.contour(x[0], x[1], pdf, levels=10,
cmap=sns.color_palette("magma", as_cmap=True))
ax.set_axis_off()


## Filled Contour Plot¶

In [4]:
fig, ax = plt.subplots(figsize=(7,7))

ax.contourf(x[0], x[1], pdf, levels=7,
cmap=sns.color_palette("crest_r", as_cmap=True))
ax.set_axis_off()


## Contour Plots with colorbar¶

In [5]:
fig, ax = plt.subplots(figsize=(7,7))

cf = ax.contourf(x[0], x[1], pdf, levels=7,
cmap=sns.color_palette("crest_r", as_cmap=True))
ax.set_xticks([])
ax.set_yticks([])
ax.set_xlabel('x')
ax.set_ylabel('y')

cbaxes = fig.add_axes([.2, .02, .6, .02])
fig.colorbar(cf, ticks=np.linspace(0., .2, 6),
orientation='horizontal', cax=cbaxes);