How To Make Density Plot in Python with Altair? Last Updated : 20 Mar, 2024 Comments Improve Suggest changes Like Article Like Report 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. First, let's import these libraries using- Python3 import pandas as p # loading pandas library import altair as a # loading altair library Next, we load the data set on which we need to use the density plot. Python3 data_set = 'insurance.csv' # dataset name d = p.read_csv(data_set) # reading the datasaet d.head() # printing the first 5 data entries Output: 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- Python3 # loading a single column into # the data frame object d = d[["charges"]] a.Chart(d).transform_density('charges', as_=['CHARGES', 'DENSITY'], ).mark_area(color='green').encode( x="CHARGES:Q", y='DENSITY:Q', ) Output: Output Complete Script: Here is the code with all the steps at one place- Python3 import pandas as p # loading pandas library import altair as a # loading altair library # download dataset from https://drive.google.com/drive/folders/1Dddv1l9hpEPVWh_uuK9Iv1A1xUNy55v7?usp=sharing # OR replace name with your own dataset. data_set = 'insurance.csv' d = p.read_csv(data_set) d = d[["charges"]] a.Chart(d).transform_density('charges', as_=['CHARGES', 'DENSITY'], ).mark_area(color='green').encode( x="CHARGES:Q", y='DENSITY:Q', ) Output: Output Comment More infoAdvertise with us Next Article How To Make Density Plot in Python with Altair? S shreyaraj1234 Follow Improve Article Tags : Python Python-Altair Practice Tags : python Similar Reads Introduction to Altair in Python Altair is a statistical visualization library in Python. It is a declarative in nature and is based on Vega and Vega-Lite visualization grammars. It is fast becoming the first choice of people looking for a quick and efficient way to visualize datasets. If you have used imperative visualization libr 5 min read Create Grouped Bar Chart using Altair in Python Grouped bar charts are a handy tool to represent our data when we want to compare multiple sets of data items one against another. To make a grouped bar chart, we require at least three rows of three columns of data in our dataset. The three columns can be used as- one for values, one for series, an 3 min read Scatter Plot with Regression Line using Altair in Python Prerequisite: Altair In this article, we are going to discuss how to plot to scatter plots with a regression line using the Altair library. Scatter Plot and Regression Line The values of two different numeric variables is represented by dots or circle in Scatter Plot. Scatter Plot is also known as a 4 min read Horizontal Stripplot with Jitter using Altair in Python Prerequisites: Altair Altair is a statistical data visualization library in python which is based on Vega and Vega-Lite visualization grammars. A Stripplot is used for graphical data analysis. Â It is a simple plot of response values in a sorted order along a single axis. The strip plot consists of 2 2 min read How To Make Stripplot with Jitter in Altair Python? Prerequisites: Altair Altair is a statistical data visualization library in python which is based on Vega and Vega-Lite visualization grammars. A Stripplot is used for graphical data analysis. Â It is a simple plot of response values in a sorted order along a single axis. The strip plot consists of 2 3 min read How To Facet a Scatter Plot with Altair? In this article, we will learn how to Facet a Scatter Plot with Altair. Let's recall some concepts : Altair is a statistical visualization library in Python. It is declarative in nature and is based on Vega and Vega-Lite visualization grammars. It is fast becoming the first choice of people looking 3 min read How to Make a Simple Histogram with Altair in Python? Prerequisites: Altair Simple Histogram is the representation of frequency distribution by means of rectangles whose width represents the class interval. Histogram is the graphical representation that organizes grouped data points into the specified range. By using histogram we can visualize large am 4 min read Area Chart with Altair in Python Prerequisite: Introduction to Altair in Python An Area Graph shows the change in a quantitative quantity with respect to some other variable. It is simply a line chart where the area under the curve is colored/shaded. It is best used to visualize trends over a period of time, where you want to see h 2 min read How to Make Overlapping Histograms in Python with Altair? Prerequisite: Altair A histogram represents data provided during a sort of some groups. It is an accurate method for the graphical representation of numerical data distribution. It is a kind of bar plot where the X-axis represents the bin ranges while the Y-axis gives information about frequency. Us 2 min read Python Altair - Scatter Plot In this article, we will learn a Simple Scatter plot with Altair using python. Altair is one of the latest interactive data visualizations library in python. Altair is based on vega and vegalite- A grammar of interactive graphics. Â Here we will import the Altair library for using it. And then we wil 2 min read Like