How to Make Overlapping Histograms in Python with 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.
Using Altair, we can make overlapping histograms or layers histograms from data that is in either wide form or long tidy form.
This will common to both forms:
- Import Libraries
- Import or create data.
- Make the data long/wide according to the method.
- Plot the histograms.
Method 1: Tidy form
- To make histogram with Altair, we are using mark_area() function. Here we specify transparency level with opacity argument and therefore the key argument that creates histogram is interpolate=’step’. Without that the histogram would appear as area chart from Altair.
- Then we specify the variables and therefore the number of bins. To differentiate between different plots alt.Color() is employed with the specific variable like multiple histograms.
Method 2: Wide form
- Often you would possibly start with data that’s in wide form. Altair has transform_fold() function which will convert data in wide form to tidy long form. This allows us to not use Pandas’ melt() function and lets us transfer the information within Altair.
- We specify the variables names that are required to reshape and names for brand spanning new variables within the tidy data.
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course