How to Make a Simple Histogram with Altair in Python?
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.
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.
- 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.
altair.Chart(name_of_dataset).mark_bar().encode(x =”name_of_col1″, y=”name_of_col2″)
Example 1: Printing the dataset of cars()
Example 2: Making the default Simple Histogram using the cars dataset.
Example 3: Making the Simple Histogram using the cars dataset by setting the bin on Acceleration.
Example 4: Making the Simple Histogram using the cars dataset by setting the bin on Horsepower.
Example 5: Making the default Simple Histogram using iris dataset on sepalLength.
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.
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.
Please Login to comment...