Exploratory Data Visualisation with Altair

Imports and DataFirst of all, we need to install and import the Altair Package.InstallationTo be able to use Altair we are required to install two sets of tools depending upon the front end we would like to use.The core Altair Package and its dependenciesThe renderer for the frontend we wish to use (i.e., Jupyter Notebook, JupyterLab, Colab, or nteract).Additionally, Altair’s documentation makes use of the vega_datasets package, and so it is included in the installation instructions below.Since I will be using Jupyter Lab(recommended), the instructions pertain to it. However, for others, visit the installation page.Altair + JupyterLabconda install -c conda-forge altair vega_datasets jupyterlaborpip install -U altair vega_datasets jupyterlabAltair + JupyterLab)conda install -c conda-forge altair vega_datasets notebook vegaor pip install -U altair vega_datasets notebook vegaIn case you are new to Jupyter Lab, it is is the next-generation web-based user interface for Project Jupyter..JupyterLab enables you to work with documents and activities such as Jupyter notebooks, text editors, terminals, and custom components in a flexible, integrated, and extensible manner..In short, it is an improvement upon Jupyter Notebooks.installation- Jupyter Labconda install -c conda-forge jupyterlaborpip install jupyterlabStarting JupyterLabjupyter lab #in the terminal#JupyterLab will open automatically in your browserImporting the Altair packageimport altair as alt# Altair plots render by default in JupyterLab and nteract# Uncomment/run this line to enable Altair in the classic notebook (not in JupyterLab)# alt.renderers.enable('notebook')# Uncomment/run this line to enable Altair in Colab# alt.renderers.enable('colab')DataData in Altair is built around the Pandas dataframe..In this demonstration we’ll use the vega datasets package, to load an example dataset..Vega-Lite is a high-level grammar of interactive graphics..It provides a concise JSON syntax for rapidly generating visualizations to support analysis.We will be using the ‘cars’ dataset that comes pre-loaded with Altair..This is a fairly known dataset in the ML community and deals with comprises fuel consumption and 9 aspects of automobile design and performance for various automobile models.from vega_datasets import datacars = data.cars()cars.head()2..ChartsThe fundamental object in Altair is the,Chart which takes a dataframe as a single argument..Let’s define a chart object.import altair as altchart = alt.Chart(cars)By itself, a chart has no meaning, and it is usually used in conjunction with data, marks, and encodings, which are inherently the core pieces an Altair chart..The format by which these are specified will look something like this:alt.Chart(data).mark_point().encode( encoding_1='column_1', encoding_2='column_2', # etc.)Since we have a knowledge of the dataset, let’s have a look at marks and encodings now.MarksMarks enables us to represent each row in the data..There are a number of available marks that can be used like point, circle, square, etc.alt.Chart(cars).mark_point()#For complete list of available marksalt.Chart.mark_ # followed by the tab keyThe above chart doesn’t convey much except that it is a stack of 406 points all laid on top of each other..Why 406?. More details

Leave a Reply