Live data from Hacker News

Vega-Altair: Declarative Visualization in Python

altair-viz.github.io

1–10 of 44 posts

Re: Vega-Altair: Declarative Visualization in Python

#2
> empowers you to spend less time writing code and more time exploring your data

Sidenote: I like Altair and think it's a good development, despite rendering being performed client side.

This said, the claim here is tiring when it's used everywhere. Having spent significant time with Altair, I'd argue it might have tighter code but the documentation can be obscure. I haven't found it to make things easier from a developer perspective, but rather it does solve the use case that you are working in Python and need to have the client render a figure without callbacks (things like raw html dumps and similar).

Re: Vega-Altair: Declarative Visualization in Python

#3
Are there non-declarative chart visualization libraries? I’ve always used matplotlib.pyplot [1] and while it doesn’t market itself as "declarative", I don’t see much difference:

     # Vega-Altair
     alt.Chart(source).mark_line().encode(
       x='x',
       y='f(x)'
     )

     # Pyplot
     plt.plot(source)
     plt.xlabel('x')
     plt.ylabel('f(x)')
[1]: https://matplotlib.org/stable/tutorials/pyplot.html

Re: Vega-Altair: Declarative Visualization in Python

#5
Working as a data scientist, I have exclusively used Altair since I joined my company in 2021. Every one of my coworkers uses Matplotlib. Two people said something like "Oh, you're using that library that's supposed to be better and probably is, but I just don't want to relearn everything for plotting" but nobody else has even shown interest, let alone taken the plunge.

If you need to learn a plotting library and you already work in Pandas, I recommend choosing Altair to learn. It's a natural extension to pd.DataFrame and the only magical incantation to learn is

    alt.Chart(df).mark_.encode(x=df["col"], y=df["other_col"])
I find this significantly easier to work with than Matplotlib, where the same things can be done in several ways with subplots, plt.figure(), df.plot(), and maybe others?

My only complaint with the library is that outputting to an image file feels weirdly complicated. I often resort to making HTML files and taking a screenshot if I don't want to take the time to look up all the steps equivalent to `.savefig("file.png")`.

Re: Vega-Altair: Declarative Visualization in Python

#6
post #3

Are there non-declarative chart visualization libraries? I’ve always used matplotlib.pyplot [1] and while it doesn’t market itself as "declarative", I don’t see much difference: # Vega-Altair alt.Chart(source).mark_line().encode( x='x', y='f(x)' ) # Pyplot plt.plot(source) plt.xlabel('x') plt.ylabel('f(x)') [1]: https://matplotlib.org/stable/tutorials/pyplot.html

If we only look at the simplest example then I would agree that there is not much difference. But more complicated plots will require you to write code in a more verbose and imperative fashion when using matplotlib.

Take a faceted plot like this scatter matrix [1] and try to plot it in matplotlib. You would need to set up the grid using subplots, then define the combinations you want and finally write logic to fill each subplot. The vega/altair code is much more declarative. You just tell it what needs to be in the rows/columns and vega/altair takes care of the rest.

[1]: https://altair-viz.github.io/gallery/scatter_matrix.html

Re: Vega-Altair: Declarative Visualization in Python

#7
Altair is so important for data science as a product.

Every data scientist endeavors to make an impact with their analysis, and ultimately that is typically tied to some kind of visualization. There needs to be a way to a) build the visualization you want and b) get it out there to people who would find it useful.

Just plotting in matplotlib means that you must either export as a PNG (ew) or provide the analysis itself to users/decision makers. PNGs are terrible because you completely lose interactivity. Providing the analysis means figuring out deployment of your python environment, which is possible but just causes another step between analysis and decision made on the analysis.

Altair and the vega-lite grammar of visualizations provides an interoperable and data centric way to build visualizations. It is extremely flexible when building visualizations and I find it very intuitive when it comes to complex plots. They can also be easily embedded into any webpage after being exported using the vega-lite spec, just include the vega-lite script in the html page. Can even be used with in dashboarding tools like Spotfire (I assume also with things like PowerBI although I haven't done it).

Imo no real reason to use matplotlib as a data scientist lest you seriously limit the future impact of your work

Re: Vega-Altair: Declarative Visualization in Python

#8
Sidenote: Has Vega* a specific reference to the "Grammar of Graphics" 2005 book [1]? I used that book in research and remember praying for a real implementation. Looking into SO and an answer appeared in 2014 [2].

[1] https://link.springer.com/book/10.1007/0-387-28695-0

[2] https://stackoverflow.com/questions/4892368/implementations-...

Re: Vega-Altair: Declarative Visualization in Python

#9
post #3

Are there non-declarative chart visualization libraries? I’ve always used matplotlib.pyplot [1] and while it doesn’t market itself as "declarative", I don’t see much difference: # Vega-Altair alt.Chart(source).mark_line().encode( x='x', y='f(x)' ) # Pyplot plt.plot(source) plt.xlabel('x') plt.ylabel('f(x)') [1]: https://matplotlib.org/stable/tutorials/pyplot.html

I don’t know what you think declarative means, but the example you’re showing is as non-declarative as it could be for such a simple thing.

Re: Vega-Altair: Declarative Visualization in Python

#10
Altair is great.

I'm using Plotly more lately, for 2 reasons - chart have very good interactivity by default and I'm also a Dash user - so it comes in naturally.

But I believe Altair is excellent for exploration - simple charts are very simple to create, and it's not complicated to create a complex exploration dashboard - with all charts linked between them - so you can select a subset of data on one chart, and it automatically updates the others. Highly recommend at least exploring it !

Post reply on HN