How To Make Density Plot in Python with Altair?
Density plots are a variation of Histograms that are used to observe the distribution of a variable in data set over a continuous interval or a time period. The peaks of a Density Plot indicate where values are concentrated over an interval.
Compared to Histograms, Density Plots are better at determining the distribution shape because they’re not affected by the number of bins.
Density Plot in Python using Altair
We can make a density plot in python using the libraries Pandas and Altair.
- Altair-It is a statistical visualization library based on Vega and Vega-lite.
- Pandas-It is an open-source data analysis and manipulation tool in Python.
Note: We will be using the ‘insurance.csv’ dataset which can be downloaded from Google Drive.
First, let’s import these libraries using-
Next, we load the data set on which we need to use the density plot.
As you can see, there are seven columns in the dataset. We shall use “charges” to make a density plot. To do, so we must first transform our data into density. This is done by using the transform_density() function. The parameters are the variable of interest and a name to indicate the transformed variable which is written as “as_=[‘Charges’, ‘density’]”. Bringing it together-
Complete Script: Here is the code with all the steps at one place-