Table of Contents
Creating a density plot in Matplotlib involves using the ‘density’ parameter in the ‘plot’ function. This parameter allows for the visualization of the distribution of data points over a continuous interval. It is useful for understanding the overall shape of the data and identifying any patterns or outliers. To create a density plot, one can use the ‘hist’ function with the ‘density’ parameter set to ‘True’ or use the ‘kde’ function specifically designed for density plots. Both methods provide customizable options for color, label, and style. Some examples of density plots in Matplotlib can be found in the official documentation or through online tutorials and guides.
Create a Density Plot in Matplotlib (With Examples)
The easiest way to create a density plot in Matplotlib is to use the function from the seaborn visualization library:
import seaborn as sns #define data data = [value1, value2, value3, ...] #create density plot of data sns.kdeplot(data)
The following examples show how to use this function in practice.
Example 1: Create Basic Density Plot
The following code shows how to create a basic density plot in seaborn:
import seaborn as sns #define data data = [2, 2, 3, 5, 6, 6, 7, 8, 9, 10, 12, 12, 13, 15, 16] #create density plot of data sns.kdeplot(data)

The x-axis shows the data values and the y-axis shows the corresponding probability density values.
Example 2: Adjust Smoothness of Density Plot
You can use the bw_method argument to adjust the smoothness of the density plot. Lower values lead to a more “wiggly” plot.
import seaborn as sns #define data data = [2, 2, 3, 5, 6, 6, 7, 8, 9, 10, 12, 12, 13, 15, 16] #create density plot of data with low bw_method value sns.kdeplot(data, bw_method = .3)
Conversely, higher values for bw_method lead to a smoother plot:
import seaborn as sns #define data data = [2, 2, 3, 5, 6, 6, 7, 8, 9, 10, 12, 12, 13, 15, 16] #create density plot of data with high bw_method value sns.kdeplot(data, bw_method = .8)

Example 3: Customize Density Plot
You can also customize the color and style of the density plot:
import seaborn as sns #define data data = [2, 2, 3, 5, 6, 6, 7, 8, 9, 10, 12, 12, 13, 15, 16] #create density plot of data with high bw_method value sns.kdeplot(data, color='red', fill=True, alpha=.3, linewidth=0)

Cite this article
stats writer (2024). How can I create a density plot in Matplotlib? Can you provide some examples?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-create-a-density-plot-in-matplotlib-can-you-provide-some-examples/
stats writer. "How can I create a density plot in Matplotlib? Can you provide some examples?." PSYCHOLOGICAL SCALES, 3 May. 2024, https://scales.arabpsychology.com/stats/how-can-i-create-a-density-plot-in-matplotlib-can-you-provide-some-examples/.
stats writer. "How can I create a density plot in Matplotlib? Can you provide some examples?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-create-a-density-plot-in-matplotlib-can-you-provide-some-examples/.
stats writer (2024) 'How can I create a density plot in Matplotlib? Can you provide some examples?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-create-a-density-plot-in-matplotlib-can-you-provide-some-examples/.
[1] stats writer, "How can I create a density plot in Matplotlib? Can you provide some examples?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, May, 2024.
stats writer. How can I create a density plot in Matplotlib? Can you provide some examples?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.

