[HN Gopher] Vega-Altair: Declarative Visualization in Python
       ___________________________________________________________________
        
       Vega-Altair: Declarative Visualization in Python
        
       Author : tosh
       Score  : 54 points
       Date   : 2024-02-25 18:12 UTC (4 hours ago)
        
 (HTM) web link (altair-viz.github.io)
 (TXT) w3m dump (altair-viz.github.io)
        
       | tomrod wrote:
       | > 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).
        
       | hk__2 wrote:
       | 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
        
         | lcvriend wrote:
         | 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
        
         | epgui wrote:
         | 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.
        
       | datadeft wrote:
       | Is there a plotting library that uses webgl? I am only aware of
       | Plotly that uses webgl for some of the graphs.
        
       | ryan-duve wrote:
       | 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_<plot_type>.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")`.
        
       | Case_of_Mondays wrote:
       | 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
        
       | wslh wrote:
       | 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-...
        
       ___________________________________________________________________
       (page generated 2024-02-25 23:00 UTC)