KDE Plot Visualization with Pandas and Seaborn
KDE is Kernel Density Estimate
used to visualize the probability density of continuous and non-parametric data variables. When you want to visualize multiple distributions, KDE
the function will produce a more concise and easier to interpret plot.
Using KDE
, we can visualize multiple data samples using a single chart, which is a more efficient way to visualize data.
Seaborn
Seaborn is a matplotlib
python library similar to . Seaborn can be integrated with pandas
and numpy
for data representation.
Data scientists use this library to create informative and beautiful statistical charts and graphs. Using these presentations, you can understand the clear concepts and information flow within different modules.
We can plot univariate and bivariate plots using KDE function, Seaborn, and Pandas.
We will learn how to use pandas and seaborn for KDE plot visualization. This article will use mtcars
several samples of the dataset to demonstrate KDE plot visualization.
Before we get started, you need to install or add the seaborn
and sklearn
libraries using pip command.
pip install seaborn
pip install sklearn
Data Visualization Using Normal KDE Plot and Seaborn in Python
We can plot the data using normal KDE plotting functions with the Seaborn library.
In the example below, we create 1000 data samples using the random library and then arrange them in numpy
an array of , as the Seaborn library is only available with numpy
and Pandas dataframes
.
Sample code:
import seaborn as sn
import matplotlib.pyplot as plt
import numpy as np
data = np.random.randn(1000)
# KDE Plot with seaborn
res = sn.kdeplot(data, color="red", shade="True")
plt.show()
Output:
We can also visualize the above data sample vertically or restore the above plot using KDE and Seaborn libraries. We use the plot attribute vertical=True
to restore the above plot.
Sample code:
import seaborn as sn
import matplotlib.pyplot as plt
import numpy as np
data = np.random.randn(1000)
# KDE Plot with seaborn
res = sn.kdeplot(data, color="green", vertical=True, shade="True")
plt.show()
Output:
Plotting 1D KDE in Python using Pandas and Seaborn
We can use KDE plots to visualize the probability distribution of a single target or a continuous attribute. In the following example, we read mtcars
the CSV file of the dataset.
There are over 350 entries in our dataset and we will visualize the univariate distribution along the x-axis.
Sample code:
import seaborn as sn
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
# read CSV file of dataset using pandas
dataset = pd.read_csv(r"C:\\Users\\DELL\\OneDrive\\Desktop\\samplecardataset.csv")
# kde plot using seaborn
sn.kdeplot(data=dataset, x="hp", shade=True, color="red")
plt.show()
Output:
You can also flip the plot by visualizing the data variable along the y-axis.
Sample code:
import seaborn as sn
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
# Read CSV file using pandas
dataset = pd.read_csv(r"C:\\Users\\DELL\\OneDrive\\Desktop\\samplecardataset.csv")
# KDE plotting using seaborn
sn.kdeplot(data=dataset, y="hp", shade=True, color="red")
plt.show()
Output:
We can visualize the probability distribution of multiple target values in a single plot.
Sample code:
import seaborn as sn
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
# Read CSV file using pandas
dataset = pd.read_csv(r"C:\\Users\\DELL\\OneDrive\\Desktop\\samplecardataset.csv")
# KDE plotting using seaborn
sn.kdeplot(data=dataset, x="hp", shade=True, color="red")
sn.kdeplot(data=dataset, x="mpg", shade=True, color="green")
sn.kdeplot(data=dataset, x="disp", shade=True, color="blue")
plt.show()
Output:
Plotting 2D or Bivariate KDE Plots in Python Using Pandas and Seaborn
We can visualize the data in a two-dimensional or bivariate KDE plot using seaborn and pandas libraries.
In this way, we can visualize the probability distribution of a given sample for multiple continuous attributes. We visualize the data along the x and y axes.
Sample code:
import seaborn as sn
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
# Read CSV file using pandas
dataset = pd.read_csv(r"C:\\Users\\DELL\\OneDrive\\Desktop\\samplecardataset.csv")
# KDE plotting using seaborn
sn.kdeplot(data=dataset, shade=True, x="hp", y="mpg")
plt.show()
Output:
Similarly, we can plot the probability distribution of multiple samples using a single KDE plot.
Sample code:
import seaborn as sn
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
# Read CSV file using pandas
dataset = pd.read_csv(r"C:\\Users\\DELL\\OneDrive\\Desktop\\samplecardataset.csv")
# KDE plotting using seaborn
sn.kdeplot(data=dataset, shade=True, x="hp", y="mpg", cmap="Blues")
sn.kdeplot(data=dataset, shade=True, x="hp", y="cyl", cmap="Greens")
plt.show()
Output:
in conclusion
We have demonstrated KDE plot visualization using Pandas and Seaborn libraries in this tutorial. We have seen how to visualize probability distributions of single and multiple samples in a 1D KDE plot.
We discussed how to visualize two-dimensional data using KDE plots with Seaborn and Pandas.
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
Convert Tensor to NumPy array in Python
Publish Date:2025/05/03 Views:85 Category:Python
-
This tutorial will show you how to convert a Tensor to a NumPy array in Python. Use the function in Python Tensor.numpy() to convert a tensor to a NumPy array Eager Execution of TensorFlow library can be used to convert tensor to NumPy arra
Saving NumPy arrays as images in Python
Publish Date:2025/05/03 Views:193 Category:Python
-
In Python, numpy module is used to manipulate arrays. There are many modules available in Python that allow us to read and store images. An image can be thought of as an array of different pixels stored at specific locations with correspond
Transposing a 1D array in NumPy
Publish Date:2025/05/03 Views:98 Category:Python
-
Arrays and matrices form the core of this Python library. The transpose of these arrays and matrices plays a vital role in certain topics such as machine learning. In NumPy, it is easy to calculate the transpose of an array or a matrix. Tra
Find the first index of an element in a NumPy array
Publish Date:2025/05/03 Views:58 Category:Python
-
In this tutorial, we will discuss how to find the first index of an element in a numpy array. Use where() the function to find the first index of an element in a NumPy array The function in the numpy module where() is used to return an arra
Remove Nan values from NumPy array
Publish Date:2025/05/03 Views:118 Category:Python
-
This article discusses some built-in NumPy functions that you can use to remove nan values. Remove Nan values using logical_not() and methods in NumPy isnan() logical_not() is used to apply logical NOT to the elements of an array. isn
Normalizing a vector in Python
Publish Date:2025/05/03 Views:51 Category:Python
-
A common concept in the field of machine learning is to normalize a vector or dataset before passing it to the algorithm. When we talk about normalizing a vector, we say that its vector magnitude is 1, being a unit vector. In this tutorial,
Calculating Euclidean distance in Python
Publish Date:2025/05/03 Views:128 Category:Python
-
In the world of mathematics, the shortest distance between two points in any dimension is called the Euclidean distance. It is the square root of the sum of the squares of the differences between the two points. In Python, the numpy, scipy
Element-wise division in Python NumPy
Publish Date:2025/05/03 Views:199 Category:Python
-
This tutorial shows you how to perform element-wise division on NumPy arrays in Python. NumPy Element-Wise Division using numpy.divide() the function If we have two arrays and want to divide each element of the first array with each element
Convert 3D array to 2D array in Python
Publish Date:2025/05/03 Views:79 Category:Python
-
In this tutorial, we will discuss the methods to convert 3D array to 2D array in Python. numpy.reshape() Convert 3D array to 2D array using function in Python [ numpy.reshape() Function](numpy.reshape - NumPy v1.20 manual)Changes the shape