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

Question:

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?

Asked By: roudan

||

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.

Answered By: joelostblom

For some scenarios one library might be more convenient than the other. For example, for plotting wide form data, I personally think it is easier to work with Plotly express because Altair requires the data to be transformed to long form first whereas in Plotly you can pass a list of columns to plot instead. I also think it is easier to plot the index with Plotly and to center titles. Here is an example that includes all three so that you see the differences:

Altair:

alt.Chart(
    df.melt(['observed', 'predicted'], var_name='category', value_name='data').reset_index(),
    title=alt.TitleParams('Product 1', anchor='middle')
).mark_line().encode(
    alt.X("index"),
    alt.Y("data"),
    alt.Color("category")
)

Plotly express:

fig = px.line(df, x=df.index, y=["observed", "predicted"])
fig.update_layout(
    title={"text": "Product 1", "x": 0.5, "xanchor": "center"},
    yaxis_title="data",
    legend_title_text="category"
)

I personally also like the documentation in Plotly more although Altair recently updated their docs which are now significantly improved. Here are the links for reading more about long and wide form data in both:

Answered By: asmaier
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.