How to Make a Simple Histogram with Altair in Python?
Last Updated :
03 Jan, 2021
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 amount of data and it’s frequency as one contiguous plot.
Installation:
For installing the altair library and vega_datasets we have the run the following command in our command prompt.
pip install altair
pip install vega-datasets
In this article we are going to plot Simple histogram with the help of altair library by using cars and iris datasets in the below various examples.
Step-by-step Approach:
- Import the library.
- Create or load the datasets.
- Select the column from the dataset on which we want to make the histogram.
- For making histogram, in altair library we have to give three important elements(altair.Chart(), mark_bar(), encode()).
- Put it in the variable let be named as “hist”.
- Then to see the plot we have to write the one line of code which is hist.show() and we are done.
Syntax:
altair.Chart(name_of_dataset).mark_bar().encode(x =”name_of_col1″, y=”name_of_col2″)
Example 1: Printing the dataset of cars()
Python
import altair as alt
from vega_datasets import data
import vega_datasets
car_data = data.cars()
display(car_data)
|
Output:
Example 2: Making the default Simple Histogram using the cars dataset.
Python
import altair as alt
from vega_datasets import data
car_data = data.cars()
hist = alt.Chart(car_data).mark_bar().encode(x = 'Acceleration' ,
y = 'count()' )
hist.show()
|
Output:
Example 3: Making the Simple Histogram using the cars dataset by setting the bin on Acceleration.
Python
import altair as alt
from vega_datasets import data
car_data = data.cars()
hist = alt.Chart(car_data).mark_bar().encode(x = alt.X( 'Acceleration' ,
bin = alt.BinParams(maxbins = 30 )),
y = 'count()' )
hist.show()
|
Output:
Example 4: Making the Simple Histogram using the cars dataset by setting the bin on Horsepower.
Python
import altair as alt
from vega_datasets import data
car_data = data.cars()
hist = alt.Chart(car_data).mark_bar().encode(x = alt.X( 'Horsepower' ,
bin = alt.BinParams(maxbins = 20 )),
y = 'count()' )
hist.show()
|
Output:
Example 5: Making the default Simple Histogram using iris dataset on sepalLength.
Python
import altair as alt
from vega_datasets import data
iris_data = data.iris()
hist = alt.Chart(iris_data).mark_bar().encode(x = 'sepalLength' ,
y = 'count()' )
hist.show()
|
Output:
We can also print the iris dataset using print(iris_data) in the program after loading the iris dataset. Our iris dataset after printing it looks like this.
Example 6: Making the Simple Histogram by setting bin and color.
Python
import altair as alt
from vega_datasets import data
iris_data = data.iris()
hist = alt.Chart(iris_data).mark_bar().encode(x = alt.X( 'sepalLength' ,
bin = alt.BinParams(maxbins = 20 )),
y = 'count()' ,color = 'species' )
hist.show()
|
Output:
In the same way we can make the simple histogram on any values of dataset and can set the color accordingly.
Example 7: Making Simple Histogram using iris dataset on petal width by setting bin and color.
Python
import altair as alt
from vega_datasets import data
iris_data = data.iris()
hist = alt.Chart(iris_data).mark_bar().encode(x = alt.X( 'petalWidth' ,
bin = alt.BinParams(maxbins = 10 )),
y = 'count()' , color = 'species' )
hist.show()
|
Output:
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...