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", shade=True, bw_adjust=.5)
plt.show()
# Add thresh parameter
sns.kdeplot(x=df.sepal_width, y=df.sepal_length, cmap="Blues", shade=True, thresh=0)
plt.show()