Top
Best
New

Posted by tosh 4 days ago

Plotnine(plotnine.org)
213 points | 65 comments
closed 2 hours ago|
Hey! I worked on the plotnine guide (https://plotnine.org/guide/). Always interested to hear what people find hard to understand about plotnine, or what they wish there were more of (e.g. examples, guide pages, api reference docs).

(Both has2k1 and I work for Posit, which supports plotnine work, but authoring its guide was mostly an act of passion for me :)

has2k1 7 hours ago||
If you already use plotnine, or if this has piqued your interest, the next release (v0.16.0) will bring nice capabilities.

You can get a sneak peek by installing the pre-release:

pip install --pre plotnine

Details here: https://github.com/has2k1/plotnine/issues/1031

Disclaimer: I'm the author.

jjgreen 6 hours ago||
Minor nit, the "Installing" link on the linked page leads to the general documentation.
x312 4 hours ago||
Love your work on this, thanks for bringing the ggplot syntax to Python!
seanhunter 5 hours ago||
Plotnine has been great in my usage, but I see violin plots on the front page. Just say no to violin plots.

In almost any situation you either want to talk about the actual distribution (in which case plotting the distribution on one side of the line arranged horizontally is significantly superior to plotting it vertically on both sides of the line for some reason as a violin plot does[1]) or you want to talk about the quartiles etc in which case a boxplot is better.

A violin plot tries to do both and as a result does them both badly.

Extended anti-violin plot rant here https://www.youtube.com/watch?v=_0QMKFzW9fw

[1] I remember in one meeting before I knew better, producing some violin plots and putting them on a slide and I knew I had gone wrong when that slide came up and everyone in the room had this confused expression on their faces and was leaning their head over to the side to try to see the distribution better. When your visualization produces obvious confusion like that, you can be completely certain it has failed.

stared 3 hours ago||
Violin plots have an interesting reputation (https://xkcd.com/1967/, https://www.reddit.com/r/labrats/comments/91ex4u/is_it_just_..., https://jabde.com/2022/12/22/banned-violin-plots/).

For showing distributions, I much prefer strip plots (https://seaborn.pydata.org/generated/seaborn.stripplot.html), perhaps with opacity, or swarm plots (https://seaborn.pydata.org/generated/seaborn.swarmplot.html) - no averaging with an unknown kernel, no hiding distributions behind a box plot, and the data is directly visible. We also directly see whether it is based on 5, 100, or many more points.

When using histograms, binning is usually more straightforward than kernels. And in any case, the mirror reflection of a histogram is not needed.

pks016 2 hours ago||
Another alternative is raincloud plot (depending on data).
suuuuuuuu 2 hours ago|||
The arguments in the video are mostly misdirected, conflations of related but distinct issues, or (hypocritically) just opinions. Here are the unpacked issues as I see it:

1. Whether to summarize data by a handful of summary statistics or a full density. Obviously, some statistics reported in isolation can misrepresent the underlying distribution, but these considerations ultimately depend on what specific point one seeks to make with a plot. There's no reason a priori that visually annotating summaries/quantiles on a distribution plot can't be helpful (quite the contrary).

2. Whether to "smooth the data" (read: perform kernel density estimation). In some sense this is a long-solved problem: there are mathematically grounded methods for estimating the optimal KDE bandwidth (with varying degrees of assumption on the underlying distribution), which are what's used by any serious plotting library. And whether authors adequately describe what they're actually plotting is a separate matter. That said, there are many reasons not to show a KDE over, say, a binned histogram, especially with raw data and/pr small sample sizes, but these are entirely orthogonal to the choice of displaying a KDE as a violin plot versus something else.

3. How to normalize densities. With raw data, you probably want to compare frequencies (a proper pdf). If displaying just a single distribution, there's obviously no reason not to show the density (it's only a trivial rescaling of the axis tick values). When comparing multiple, the decision again depends on the point of the plot. Losing dynamic range for broader densities when compared with narrower ones can be counterproductive. E.g., in Bayesian parameter inference (where the data are MCMC samples), we almost never compute the actual normalization factor, but rather want to compare relative probabilities (i.e., within a single distribution) of different parameter values across different distributions. Of course, nothing forces one normalization over another for violin plots.

All of those are separate (and rectifiable!) issues from the defining characteristics of a violin plot:

1. Distributions displayed vertically rather than horizontally (both being harder to interpret and inappropriately suggestive). We almost exclusively visualize functions plotted vertically across a horizontal coordinate. I think this is the only valid, specific criticism of the common violin style itself, but the fix is of course trivial.

2. Horizontal violin are then only different from a ridge plot by a) not overlapping (which to me is a major improvement over standard ridge plots, but also trivially fixed) and b) being displayed symmetrically. I find it slightly easier to compare relative heights in the symmetric version, especially when comparing many distributions (such that each is relatively narrow). Even if not, the difference is so superficial/trivial that I don't find it worth arguing about.

Beyond this, the video's main argument (repeated every minute) seems to be that "it's bad, it's just bad", but there are only so many ways to make a 5 minute argument fill a 42 minute video. (This style of video is so grating to me.)

mathisfun123 1 hour ago||
You couldn't pay me enough to be this opinionated about something so banal.
MoonWalk 4 minutes ago||
Is what?
williamcotton 5 hours ago||
I've always liked the ggplot2 and the Grammar of Graphics approach to plotting so much so that I wrote my own DSL based on it - it is standalone, written in Rust, has WASM bindings (as you can see on the website) and more:

https://williamcotton.github.io/algraf

It pairs well with a related data translation DSL:

https://williamcotton.github.io/pdl

And you can see the two working together here:

https://williamcotton.github.io/datafarm-studio

There's LSPs for both, LSP clients for VS Code, and even language diagnostics for standalone Monaco editors in the browser.

Of note is that the same language diagnostics are exposed via the WASM as via the LSP interface allowing for the same friendly red squiggles to look and work the same in both your browser with Monaco and your editor with the LSP!

stared 3 hours ago||
A year ago, added R to the pipeline (with multiple complications) just to use ggplot2 - even though Python was the main tech.

https://quesma.com/blog/sandboxing-ai-generated-code-why-we-...

Good, that ggplot2 can run inside in WASM, vide https://github.com/QuesmaOrg/webr-ggplot-playground

williamcotton 1 hour ago||
A big part of the motivation was that something like this...

  $ which ggplot
  ggplot () {
          if [[ "$1" == "-f" ]]
          then
                  shift
                  rush run --library tidyverse "$(cat "$1")" -
          else
                  rush run --library tidyverse "$@" -
          fi
  }

  $ echo "one,two,three\n1,2,3\n4,5,6\n,7,8,9" | ggplot 'ggplot(df, aes(one, two)) + geom_col() + theme_minimal()' | imgcat
...is just very slow. Booting R just to run ggplot2 was not cutting it compared to a custom DSL written in Rust!

BTW, that "R on the command line" tool was inspired by:

https://datascienceatthecommandline.com

tmoertel 4 hours ago|||
Nifty! What motivated you to create these tools?
williamcotton 4 hours ago||
I really like to build things that build other things!
fifilura 1 hour ago||
It is great, but I have completely given up non-interactive plots since a while.

You get so much more information in plots using bokeh (or I assume plotly).

Tooltips, zooming, interaction.

And the LLM helps a lot when the plot is a bit more complex.

qrobit 9 hours ago||
And it comes with tidyverse-like cheatsheet[1] that I confused with ggplot2 when first discovered plotnine

[1]: https://github.com/rstudio/cheatsheets/blob/main/plotnine.pd...

has2k1 7 hours ago|
Sorry for the confusion. Though, it is a mango tree in a mango garden! The continued development and maintenance of plotnine is supported by Posit, PBC, the same company behind the Tidyverse.

Disclaimer: I am the author.

kasperset 3 hours ago||
While it is good to have plotnine for python but ggplot2 in R has this whole ecosystem of packages/extensions that augment the ggplot2 such as ggalluvial, ggrepel. More at this link https://exts.ggplot2.tidyverse.org/gallery/ This would make it really compelling to tip my toes in the Python world. Regardless, I am liking this "ggplotification" of python. All we need is more "tidification" of Python data frame world.
lacoolj 1 hour ago||
If this was for JS and not Python I would be all over it for the upcoming NFL/NBA seasons and my stat visualizations
icarusz 3 hours ago|
matplotlib is what most folks reach for by default, but I'd argue that's habit and popularity talking, not a real fit advantage. For a lot of plotting work, the grammar-of-graphics libraries — Altair, plotnine — may be the better tool for many people (and Agents); they just don't have the muscle memory behind them.

(Disclosure: I'm at Posit, which supports plotnine.)

jllyhill 3 hours ago||
What would you say are the benefits of the grammar-of-graphics approach? I've been working with plotting for more than a decade now and have never heard of it. Right now I'm looking through the gallery and can't really grasp what makes this approach better than the one in matplotlib.

PS. It took someone in the comments writing "import plotnine as p9" for me to understand it isn't plotLine.

icarusz 3 hours ago|||
It has to do with the structure of the information/inputs. Its more a DSL with a structure rather than a jumble of API arguments. So first few times out it may take some getting used to, but as that structure becomes clear it becomes more intuitive. Thats also why even with way more matplotlib examples in the world, LLM agents do better "guessing" how to use plotnine... or at least it was that way a few months ago when we were testing them head to head.
icarusz 1 hour ago||
[flagged]
epgui 1 hour ago|||
> What would you say are the benefits of the grammar-of-graphics approach?

When a mathematical formalism exists, just use that. Other approaches just reinvent the wheel on an ad-hoc/piecemeal basis and end up making all sorts of unnecessary compromises.

SubiculumCode 3 hours ago||
It supports plotnine via plot display window, do you mean?
More comments...