Using Altair for Creating Data Visualizations

Data Visualization helps unravel the hidden data patterns that naked eyes cannot see in a tabular format. It helps in understanding correlation and association between different data points. Different types of visualization help in understanding the different properties of the dataset.

Python provides many libraries that can be used for data visualization. Every package has its own pros and cons. Here we will discuss Interactive Data Visualization which is possible only in very few pythons libraries.

Altair is an open-source Python library that is used for creating Data Visualizations that are highly interactive and useful. It provides a wide variety of graphs and plots that we can create.

In this article, we will explore Altair and create some visualizations using it.

Let’s get started…

We will start by installing Altair using pip installation. The command given below will install Altair using pip.

pip install altair vega_datasets

In this step, we will import all the libraries that are required libraries for creating the models and then visualizing those models.

import altair as alt
from vega_datasets import data

For this article, we will the famous dataset ‘Cars’ from the Vega datasets. In the code given below, you can see how we can import the dataset.

source = data.cars()

Now we will start by creating some charts and explore how to create these charts.

  1. Scatter Plot
alt.Chart(source).mark_circle(size=60).encode(
x='Horsepower',
y='Miles_per_Gallon',
color='Origin',
tooltip=['Name', 'Origin', 'Horsepower', 'Miles_per_Gallon']
).interactive()

Here you can clearly see that we added a tooltip in the code which makes this graph more interactive.

2. Bar Plots

alt.Chart(source).mark_bar().encode(
y='Origin:N',
color='Origin:N',
x='count(Origin):Q'
)

3. Combined Charts

points = alt.Chart(source).mark_point().encode(
x='Horsepower:Q',
y='Miles_per_Gallon:Q',
color=alt.condition(brush, 'Origin:N', alt.value('lightgray'))
).add_selection(
brush
)
bars = alt.Chart(source).mark_bar().encode(
y='Origin:N',
color='Origin:N',
x='count(Origin):Q'
).transform_filter(
brush
)
points & bars

The chart created using the code above is highly interactive as you can see in the video above. This graph has both a scatter plot and a bar plot.

Here you can clearly visualize different charts and plots that we created using Altair. Try this with different datasets, create…

Continue reading: https://towardsdatascience.com/interactive-data-visualization-2c7d62fb3b16?source=rss—-7f60cf5620c9—4

Source: towardsdatascience.com