Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Plotly express vs. Altair/Vega-Lite for interactive plots

Recently I am learning both Plotly express and Altair/Vega-Lite for interactive plotting. Both of them are quite impressive and I am wondering what their strengths and weaknesses are. Especially for creating interactive plots, are there any big differences between them and when is one more suitable than the other?

like image 494
roudan Avatar asked Jan 21 '20 16:01

roudan


People also ask

Is Plotly interactive?

Plotly produces interactive graphs, can be embedded on websites, and provides a wide variety of complex plotting options. The graphs and plots are robust and a wide variety of people can use them. The visuals are of high quality and easy to read and interpret.

What is better than Plotly?

This is obvious, but Matplotlib is way more popular than Plotly. The main advantage of being so popular is that notebooks using Matplotlib will be easily reproduced by other people since different people's chances of having it installed are higher.

What is the difference between Plotly and Plotly Express?

Plotly Express is a plotly. express module (usually imported as px) that contains functions that can create entire figures at once. Plotly Express is a part of the Plotly library that comes pre-installed and is the recommended starting point for most common figures. Every Plotly Express function returns a plotly.


1 Answers

Trying to not get into personal preferences and too many details, here are some of the main similarities and differences between the two as far I am aware.

Design principles

Both Plotly express and Altair are high level declarative libraries, which means you express yourself in terms of data and relationships (like in seaborn, holoviews, and ggplot) rather than in terms of lower level plotting mechanics (like in matplotlib and bokeh). This requires less typing and lets your focus be on the data, but you also have less control of exact details in the plot.

Both are interactive plotting packages based on underlying javascript libraries. Plotly express sits on top of plotly.py which is a Python wrapper for plotly.js whereas Altair is a wrapper around VegaLite.js which in turn is based on Vega.js. Both plotly.js and Vega are based on the D3 visualization library, which is the standard js viz library.

Syntax

One of the more fundamental differences is in the syntax. Plotly's syntax is more focused on having individual functions for each plot and then that function takes several parameters to control its behavior. For example, the violinplot function has a parameter for whether there should be a strip plot included as well. Altair focuses on having a graphical grammar where you compose charts from individual graphical grammar units just as you compose sentences from words. For example, if I wanted to combine two charts in Altair I would create them individually and add the them together via the layer operator (this is possible to an extend in Plotly also but not always straightforward with Plotly express). So Altair's syntactical principles are very similar to ggplot, whereas Plotly express is more (but not quite) like seaborn in its syntax.

Interactivity

Both are very capable and can create multipanel layouts of plots that are linked together via interactions, such as filtering or hover events that update the other plots. All interactivity in the core libraries themselves is client-side (happens in your browser, and still present when exporting a notebook to HTML). Server-side interactivity (requires a running Python server) can be achieved by pairing with an external dashboarding solution, which allows you to trigger a custom function to execute on a selection of points in a plot. For Plotly, this is their own solution Dash and for Altair this has recently been added in the Panel dashboarding library (and it might be implemented for Streamlit in the future).

Altair is the only visualization package that I know of that has an interaction grammar, which allows you to compose interactions between widgets and plots according to similar principles as when creating the plots via the grammar of graphics. This makes the experience of creating plots consistent and can allow for increased creativity and flexibility when designing interactions. Plotly has support for animations in an intuitive way, and this can be a great if your data is a time series or similar.

Appearance

Please check out the Altair and Plotly express galleries to decide which aesthetics you prefer. Many of the defaults (background color, mark sizes, axis number, etc) are of course changeable (individually or via themes), but you will still get a good general idea for how your plots will look from spending time in the galleries.

One notable difference is that Altair will keep plot elements and spacing constant while resizing the plot size to fit e.g. more categorical entries, whereas Plotly will modify spacing and the size of the elements in a plot to fit an overall plot size. For faceted subplots, Altair will keep each subplot a constant size and expand the total size of the chart as more are added, whereas Plotly will fit the subplots into the overall size of the plot and make each plot smaller as more are added. You can adjust both libraries to create plots of the size you want, but this is how they behave out of the box.

Extras

Plotly currently supports more many more types of graphs and has some special functionality targeted to for example biological plots and image analysis. Plotly can accelerate performance with WebGL for certain types of plots, whereas Altair's performance can be scaled with VegaFusion. Both can also work with Datashader to some extent, but not as seamlessly as when using it with Bokeh/Holoviews.

Plotly was created by a company that offers enterprise supports for some of its products. Vegalite was developed by the same research group that developed D3. Both are open source.

like image 102
joelostblom Avatar answered Sep 21 '22 10:09

joelostblom