The following code produces 3 contour plots using seaborn python library. You have to provide 2 numerical variables as input (one for each axis). The function will calculate the kernel density estimate and represent it as a contour plot or density plot. The aguments of the function kdeplot() are:

  • x, y : Variables that specify positions on the x and y axes.
  • shade : Controls the presence of a shade.
  • cmap : Colormap.
  • bw_adjust : Bandwidth, smoothing parameter.
  • thresh : number in [0, 1], Lowest iso-proportion level at which to draw a contour line.
# libraries & dataset
import seaborn as sns
import matplotlib.pyplot as plt
df = sns.load_dataset('iris')
 
# set seaborn style
sns.set_style("white")

# Basic 2D density plot
sns.kdeplot(x=df.sepal_width, y=df.sepal_length)
plt.show()
 
# Custom the color, add shade and bandwidth
sns.kdeplot(x=df.sepal_width, y=df.sepal_length, cmap="Reds", fill=True, bw_adjust=.5)
plt.show()

# Add thresh parameter
sns.kdeplot(x=df.sepal_width, y=df.sepal_length, cmap="Blues", fill=True, thresh=0)
plt.show()

Contact & Edit


👋 This document is a work by Yan Holtz. You can contribute on github, send me a feedback on twitter or subscribe to the newsletter to know when new examples are published! 🔥

This page is just a jupyter notebook, you can edit it here. Please help me making this website better 🙏!