In this tutorial, we will learn about what is contour plot and how to install Seaborn Library. We will also learn about different methods to plot contour plots.
What is a contour plot?
It is one of the data visualization techniques that is used to represent three-dimensional data in a two-dimensional plane. It can be visualized using matplotlib as well as the seaborn library in Python. We will see different methods to visualize contour plots using Seaborn.
Installing Seaborn Library
pip install seaborn
This will install the latest version of the Seaborn library in your system.
Different methods to create contour plots
1) Using a joint plot
It is used to visualize the joint distribution of given two variables. We can plot it using the following code
Importing required libraries
import seaborn as sns import numpy as np import matplotlib.pyplot as plt
In the above code seaborn library that we are importing, we use it to visualize joint plots. Numpy to perform operations on numeric data. Matplotlib to customize the graph.
Creating random values for our variables
np.random.seed(42) x1 = np.random.randn(1000) y1 = np.random.randn(1000)
In the above we are setting the seed as 42 to ensure each time we run the code, we will get the same sequence of random numbers. Then usinsg np.random.randn function we are generating a random sequence of numbers. Here we are generating just random data. You can use your data in Excel or CSV file using a pandas data frame.
Plotting joint plot
g = sns.jointplot(x=x1, y=y1,kind="kde",) plt.show()
In the above code, we are providing parameters x and y with variables x1 and y1 respectively. We are setting kind=’kde’ means kernel density estimation distribution of variables x and y. We are not using the z variable here as it will automatically be created by this KDE function.
Output
2) Using kdeplot
It directly uses the kernel density estimation(KDE) function for the joint distribution of x and y. KDE is used when the distribution function of a variable is not known.
We can plot contour using the following code
sns.kdeplot(x=x1,y=y1) plt.show()
Output
As in the previous code, we are providing values to the x and y parameters.