L. Marsden
L. Marsden

Reputation: 21

Scatterplot with contour heat overlay using matplotlib

I have a large series of data which includes >200k values.

The goal is to show the spread of the data and the rough trend rather than individual points. I would like to add a contour heat overlay, similar to this question, but using matplotlib, and with the option to specify the number of contours and their intervals:

https://stats.stackexchange.com/questions/31726/scatterplot-with-contour-heat-overlay

I hope this is clear. Please let me know if not!

Upvotes: 1

Views: 2872

Answers (1)

KRKirov
KRKirov

Reputation: 4004

Seaborn's kdeplot will allow you to plot only shades rather than plotting all data points. The number of contours can be adjusted by specifying the n_levels parameter. For smaller data sets overlaying a jointplot and a kdeplot allows to display both data points and contour lines. The figure aesthetics can be varied widely, therefore I have compiled a few different options below. The subplot with the jointplots has been produced using the ImportanceOfBeingErnest's answer to this question.

import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
mean = [0, 0]
cov = [[50, 0], [0, 100]] 
x, y = np.random.multivariate_normal(mean, cov, 5000).T

# For large data sets
plt.subplots(1, 2)
plt.subplot(1, 2, 1)
cmap = sns.cubehelix_palette(as_cmap=True, dark=0, light=1, reverse=True)
a = sns.kdeplot(x, y, n_levels=20, shade=True, cmap=cmap, gridsize=200, cbar=True)
a.set_xlabel('x')
a.set_ylabel('y')
plt.subplot(1, 2, 2)
b = sns.kdeplot(x, y, n_levels=60, shade=True, gridsize=100, cmap="RdBu_r", cbar=True)
b.set_xlabel('x')
b.set_ylabel('y')

enter image description here

# For smaller data sets - jointplot and kdeplot overlayed. 
# Plain overlay
c = (sns.jointplot(x, y, color="g", marker='.').plot_joint(sns.kdeplot, n_levels=20)).set_axis_labels('x', 'y')
# with shade=True
d = (sns.jointplot(x, y, color="g").plot_joint(sns.kdeplot, n_levels=20, shade=True)).set_axis_labels('x', 'y')
# with transparency control, alpha=
e = (sns.jointplot(x, y, color="g", marker='.').plot_joint(sns.kdeplot, n_levels=20, shade=True, alpha=0.5)).set_axis_labels('x', 'y')
# diverging colour palette - blue to red
f = (sns.jointplot(x, y, marker='.').plot_joint(sns.kdeplot, n_levels=20, cmap="RdBu_r")).set_axis_labels('x', 'y')
# diverging colour palette and shade and reg
g = (sns.jointplot(x, y, marker='.', kind='reg').plot_joint(sns.kdeplot, n_levels=20, cmap="RdBu_r")).set_axis_labels('x', 'y')
# diverging colour palette, shade, 
h = (sns.jointplot(x, y).plot_joint(sns.kdeplot, n_levels=20, shade=True, cmap="RdBu_r")).set_axis_labels('x', 'y')

enter image description here

Upvotes: 1

Related Questions