Declarative visualization with vega and altair

Until now, we have used the matplotlib library, via the built-in pandas interface. matplotlib is powerful and essential to Python's data visualization ecosystem. It is not, however, the only visualization library we can use. In fact, there are a plethora of visualization tools, different in their format, focus, or even philosophy. In this section, we'll introduce you to a different tooland different concept of data visualization—and that is altair, which is a Python library based around the Vega engine. What makes it so different? A couple of things, in fact.

First of all, its core philosophy is based on the declarative approach, which can be boiled down to the following principle: the core idea is to write each chart in code as a declarationbasically, a recipe. This declaration would define what to do with the data but doesn't have to include data, implementation, or aesthetical featurescolors, fonts, and more. There are a few outcomes of that approach:

  • First, the declaration itself is a plain JSON file that can be written either manually or in any language. Hence, Vega is language-agnostic in its nature. In our case, Altair is a Python-side declaration generator; the declaration is then passed to the Vega engine, so that we don't need to work (or even know) JavaScript, per se. Also, because of the way the declaration works as an intermediate, the resulting visualization can be easily published as a standalone chart.
  • Second, the default engine of Vega is based on JavaScript (D3) and supports svg and canvas (raster). However, as Vega's interface is defined as a mini-language, decoupled of the specific implementation, there can be other engines. In fact, there are a few alternative engines already.
  • Because both style and data can be stored independently of the declaration, Vega is perfect for branded visualizationsas a user, you won't need to even think of the style guides, and designers can iterate over designs independently. Similarly, as data can be stored separately, one visualization can be re-used for other datasets or can even be used to show live datayou just need to update the linked dataset.
  • Vega also supports advanced interactions, allowing the developer to build complex systems without going into specifics. This interaction can be very useful for exploration analysis!

Let's test Altair out by replicating the charts we just built. We always start by creating an Altair Chart object:

import altair as alt

chart = alt.Chart(data)

Next, we define the type of marksfor example, to build a scatterplot, we use circle marks. Here, we can provide the properties of the mark, which are not dependent on the data:

chart = chart.mark_circle(size=60)

Finally, we add encodings, defining which column in the data should be encoded as x coordinates and which features to show in the tooltip. If we want the model to be interactive, all we need to do is call an interactive() method at the end. By default, this will add zoom and pan capabilities. We'll also pass the result property, representing the winner of a particular battle, to be encoded as shapes. Here is the code:

chart.encode(
x='allies killed',
y='axis killed',
shape='result',
tooltip=['name', 'allies killed', 'axis killed', 'start']
).interactive()

And here is the outcome! This diagram is similar to the one we made in matplotlibbut this time, we can zoom, pan, and hover over specific points, allowing us to better explore the data:

In the making of this chart, we had to overwrite the chart object multiple times; usually, most of the settings are done in one piece, like this:

chart = alt.Chart(data).mark_circle(size=60).encode(
x='allies killed',
y='axis killed',
color=alt.Color('parent', legend=None),
tooltip=['name', 'allies killed', 'axis killed', 'start']
).interactive()

The great feature of Altair's design is that all of the properties can be overwrittenand the chart object will always return the resulting object; hence, we can create the templates, swapping only what we need for a particular visualization.

Note the triple-dot button in the upper-right corner of the chart—this menu (which, by the way, can be removed) offers to export the chart into a vector or raster, open it in the online editor, or store the specification.

What else can we visualize with Altair? A lot! For a fair comparison, let's draw another map of the battles.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.12.108.18