5 ms·
Plotnine
- jstanley 4mo agoUsing operator overloading of "+" to configure the plot is... a choice.
- jamessb 4mo agoPlotnine is heavily inspired by the ggplot2 library, which uses the + operator in the same way: https://ggplot2.tidyverse.org/#usage https://ggplot2.tidyverse.org/#usage
- bittumenEntity 4mo agoIt is! And that's kinda the point. Like others mention, this is inspired by ggplot2, a Grammar of Graphics library. The whole idea is graphics are composed by adding "layers", not like layers on a canvas, but like pouring paint into a pot, then the library understands the content and paints it to the canvas. Layers might be pure data, geometry (lines, points, ...), annotations, styles, axis, etc. When you get familiar with it, it's much more natural way of describing plots, better composition and easier exploration
- bittumenEntity 4mo agoAnd a side note, R actually includes a pipe operator (as in Unix `|`), and said if they'd know about it at the time, they would have never used the `+` operator
- bbminner 4mo agoHow `plot(.. + ggsize(700, 300) + ..)` is superior to a keyword parameter `plot(.. , size = (700, 300), ..)`?
- 14113 4mo agoBecause, in the latter case, you have to declare a function argument for /every possible option/ that you want your graphics API to expose, and you need to do this every time you add a new option. On the other hand, declaring the options through composition means that the API for "plot" remains static, and adding/removing options can be done trivially without an API change. Composition (rather than parameters) is also more flexible. Let's say you want to divide your plot into three sub-plots, two of which are 200x200, and another which is 200x400. How do you express this as a keyword parameter? In composition, you could do something like: plot( ggsubplot(ggvsplit(ggsize(200,400), gghsplit(ggsize(200,200), ggsize(200,200)))) )
- jstanley 4mo agoAnd how do you express that as arithmetic? I can get as far as `plot() / 3` but then no idea how to proceed. I don't think overloading arithmetic is a very good way to express this.
- bittumenEntity 4mo agoIsn't this somewhat the point of python's override capability? You can exactly define what addition means here, just like you have to define what addition means for a matrix or another data structure. Separately you might find this to be smelly design, but then you should remember that this at least has precedence ¯\_(ツ)_/¯
- sdenton4 4mo agokwargs exists, and is rather more pythonic. Just pass the kwargs dict to a standard formatting function, et voila.
- bittumenEntity 4mo agoSadly, this drive towards the API design that matplotlib resembles, the raison d'etre of this library is the unusual (imho, great) API
- bbminner 4mo agoCould you elaborate on what exactly does such syntax provide over kwargs? To me it looks like verbatim x=a, y=b replaced by x(a)+y(b) in the argument section.
- stjofr 4mo ago[dead]
- CuriouslyC 4mo agoHonestly operator overloading is almost always a bad choice IMO. There are cases (e.g. matrix math) where it's the right choice because of semantic clarity, but the indirection it creates in exchange for readability is costly. It's always obvious when a function is being called, and how to navigate to the implementation, but the same is not true of overloaded operators.
- domoritz 4mo agoFor another grammar-of-graphics-based visualization library (flexibly compose charts rather than simply pick a template), check out Altair https://altair-viz.github.io https://altair-viz.github.io.
- piqufoh 4mo ago`from plotnine import *` ... I love the idea of a new python plotting library, but why is this anti-pattern so common with plotting libs?
- jamessb 4mo ago> a new python plotting library Whilst it's still not yet at 1.0.0, it's not that new: the first (0.1.0) release was in 2017: https://pypi.org/project/plotnine/#history https://pypi.org/project/plotnine/#history
- LoganDark 4mo agomatplotlib's first release was in 2003, making it more than twice as old.
- globular-toast 4mo agoBecause it's aimed at data scientists who would rather be using R...
- teruakohatu 4mo agoBecause most of the time this will be used is not part of a software development project but rather producing publication plots in a script or plots in a notebook. Not what you would want to do when incorporating it into a web app.
- lmc 4mo agoEven in a notebook it's a pain... import plotnine as p9 would be nicer.
- jeroenjanssens 4mo agoWhile it’s generally considered to be bad practice to import everything into the global namespace, I think it’s fine to do this in an ad-hoc environment such as a notebook as it makes using the many functions plotnine provides more convenient. An additional advantage is that the resulting code more closely resembles the original ggplot2 code. Alternatively, it’s quite common to `import plotnine as p9` and prefix every function with `p9`. Disclaimer: I made the plotnine homepage and cheatsheet.
- globular-toast 4mo agoBack when I did a lot of data stuff I used ggplot in R because it seemed to be popular, but I was just copy/pasting examples. Then one day I finally started to "get it" and actually read the manual. Learning the grammar of graphics was like a super power. I got to the point I could open pretty much anything people sent me and visualise it in a matter of seconds. Although I've used Python professionally a lot more than R, I still felt like R was better at this. Somehow opening files in Python always feels a bit more "heavy". I don't really know why, though.
- vshulcz 4mo ago[flagged]
- qrobit 4mo agoAnd 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.pdf https://github.com/rstudio/cheatsheets/blob/main/plotnine.pd...
- has2k1 4mo agoSorry 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.
- jpcompartir 4mo agoAfter plotnine, with a solid & performant (more than the R versions) Python version of Purrr and Dplyr I might never reach for R again!
- has2k1 4mo agoIf 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 https://github.com/has2k1/plotnine/issues/1031 Disclaimer: I'm the author.
- a_ba 4mo agoI have used neither in quite a while now but there is an alternative from jetbrains that i started using because it shares the same ergonomics and had better (?) documentation. https://lets-plot.org/python/ https://lets-plot.org/python/
- ForceBru 4mo agoIt seems like the major difference between plotnine and lets-plot is that plotnine wraps Matplotlib (and thus works everywhere Matplotlib is available, but doesn't offer much interactivity), while lets-plot is written in Kotlin and seems to provide interactive plots.
- quincymac 4mo agoNice one. I'm aware of plotnine because ggplot is more intuitive IMHO than say matplotlib. 2 things that would be awesome are interactive plots (hover + text box) and chlorpleth (tiled map) plots. On closer look you have already nailed the latter!
- williamcotton 4mo agoI'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 https://williamcotton.github.io/algraf It pairs well with a related data translation DSL: https://williamcotton.github.io/pdl https://williamcotton.github.io/pdl And you can see the two working together here: https://williamcotton.github.io/datafarm-studio 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!
- deleted 4mo ago[deleted]
- tmoertel 4mo agoNifty! What motivated you to create these tools?
- williamcotton 4mo agoI really like to build things that build other things!
- stared 4mo agoA 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-moved-from-webr-to-aws-lambda/ 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 https://github.com/QuesmaOrg/webr-ggplot-playground
- williamcotton 4mo agoA 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 https://datascienceatthecommandline.com
- vlad-asis 4mo agoI always like to see Hadley Wickam’s masterpiece frameworks emerging around.
- pwillia7 4mo agoVery cool will have to check this out more. Semi related -- I made a little d3.js AI wrapper that works pretty well for making quick charts -- https://prompt2chart.com/share/e998a3f6-9482-4c18-931f-a4513ce66b97 https://prompt2chart.com/share/e998a3f6-9482-4c18-931f-a4513...; https://prompt2chart.com/ https://prompt2chart.com/;
- seanhunter 4mo agoPlotnine 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 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 4mo agoViolin plots have an interesting reputation (https://xkcd.com/1967/ https://xkcd.com/1967/, https://www.reddit.com/r/labrats/comments/91ex4u/is_it_just_me_or_do_these_graphs_look_like_vulvae/ https://www.reddit.com/r/labrats/comments/91ex4u/is_it_just_..., https://jabde.com/2022/12/22/banned-violin-plots/ 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 https://seaborn.pydata.org/generated/seaborn.stripplot.html), perhaps with opacity, or swarm plots (https://seaborn.pydata.org/generated/seaborn.swarmplot.html 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 4mo agoAnother alternative is raincloud plot (depending on data).
- skimmed 4mo agoNice package! A side-by-side comparison with seaborn would be very nice to see.
- wygzy 4mo agoWonderful!!!
- jdthedisciple 4mo agofinally a mature matplotlib alternative?
- ForceBru 4mo agoSeems like plotnine renders plots using matplotlib and has matplotlib as a dependency: https://github.com/has2k1/plotnine/blob/f6f5cb424f38329c52672d5b9dc33684bd73e39b/pyproject.toml#L26 https://github.com/has2k1/plotnine/blob/f6f5cb424f38329c5267...
- kasperset 4mo agoWhile 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/ 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.
- persedes 4mo agoLove plotnine when I switched over to python and great to see the project develop! But I have to admit I ended up switching to altair after all which has been my go to in python now.
- trostaft 4mo agoI've been curious of other publication quality figure generation libraries for python, if you have a moment I'd be interested as to why you chose Altair over plotnine? I'm well and truly entrenched in the Matplotlib style of plotting, so I appreciate any opinions here.
- icarusz 4mo agomatplotlib 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.)
- SubiculumCode 4mo agoIt supports plotnine via plot display window, do you mean?
- jllyhill 4mo agoWhat 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 4mo agoIt 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 4mo ago[flagged]
- epgui 4mo 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.
- insane_dreamer 4mo agoHow does it handle realtime plotting?
- datadrivenangel 4mo agoPlotnine is my favorite python viz package, because ggplot2 is just so good. Altair and Bokeh are also quite good for interactive graphs, but plotnine is so ergonomic.
- stared 4mo agoSee also Gribouille: A Grammar of Graphics for Typst, discussed here a week ago https://news.ycombinator.com/item?id=48541062 https://news.ycombinator.com/item?id=48541062.
- closed 4mo agoHey! I worked on the plotnine guide (https://plotnine.org/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 :)
- CuriouslyC 4mo agoIs the selling point for this vs e.g. Plotly just the ggplot style semantics?
- epgui 4mo agoThe selling point is the grammar of graphics. See Wilkinson on the subject.
- fifilura 4mo agoIt 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.
- lacoolj 4mo agoIf this was for JS and not Python I would be all over it for the upcoming NFL/NBA seasons and my stat visualizations
- MoonWalk 4mo agoIs what?
- theLiminator 4mo agoHow does this compare with vega/vega-lite?
- dang 4mo agoRelated. Others? Plotnine: Grammar of Graphics for Python (2019) - https://news.ycombinator.com/item?id=25948840 https://news.ycombinator.com/item?id=25948840 - Jan 2021 (37 comments) Plotnine: A grammar of graphics for Python - https://news.ycombinator.com/item?id=14430447 https://news.ycombinator.com/item?id=14430447 - May 2017 (38 comments)
- epistasis 4mo agoThis is definitely one of the better Python plotting libraries out there, but no matter which one I use, they make me yearn for R. The R language is just so much more beautiful than Python, in its full lispy goodness, with lazy evaluation. It doesn't matter quite as much now that I have LLMs write a good chunk of the plotting code. But the LLM python plotting code is easily 5x-10x as many lines as a hand-crafted R plot, making it slower and harder to grok than R.
- TrackerFF 4mo agoThis is going to sound superficial as hell, some things are just horrendously ugly in R. Like the various pipe operators. %>% might just be the ugliest I've ever seen, in any language (and, yes, I know why you need % %).
- levocardia 4mo ago|> works too
- epistasis 4mo agoI guess my concept of beauty has more to do with the structure of the code, how easy it is to extend the current code to something else, the signal-to-noise ratio, etc. The visual impact of a %>% or a |> or whatever fades within a few minutes of reading code for me, what I'm looking at for beauty relates more to the AST structure. Lack of repetition, ease of understanding the data flow, ability to adapt the data flow, etc. that's what makes code beautiful to me.
- jablongo 4mo agoI use agents more and more for generating and refining plots, and its difficult for me to see the difference between matplotlib, seaborn, and plotnine, when used in this way. Agentic coding seems especially well suited to working with scientific figures, and that performance seems more or less agnostic to the underlying framework used (though they seem to prefer matplotlib, given the amount of training data for using that tool). I'm totally open to the idea that better libraries will lead to better outcomes from agents, but I haven't seen that with plotting yet.