Hacker Newsnew | past | comments | ask | show | jobs | submitlogin
Python Chart Examples (python-graph-gallery.com)
50 points by holtzy on April 16, 2024 | hide | past | favorite | 25 comments


The problem with matplotlib has never been its capability. I consider its rendering backend and resulting image quality to be top-notch, and there are a billion options for you to customize.

That second part is the biggest problem with matplotlib: the imperative API cloned from Matlab is just terrible. The vast majority of matplotlib examples are rife with global states and overriding behavior (ever have to set font size twice to force it?) At first I thought that it's a bit clunky due to my inexperience, but no, the API is just that bad. See https://ryxcommar.com/2020/04/11/why-you-hate-matplotlib/

Now, I mean no hate to matplotlib, which is still a solid library. I appreciate their commitment to backward compatibility by keeping all those baggage, and I don't want them to yank them out, given that seaborn already mitigates most of the API problems, and Altair already brings enlightenment to Python plotting.

Now if only seaborn works with polars natively ...


The object-oriented API and its documentation have improved markedly over the years, I encourage you to give it a try. Most of the official documentation examples use it now. Yes, there are lots of examples out there using the pyplot interface still, but there is enough information on the OO API now that you can get by.

Personally I think pyplot is still a perfectly fine interface for just banging out quick data visualization without too much effort, but I find myself very quickly switching to the OO interface as soon as I want a little bit of control over what's going on.

There are still some lingering issues, like the interface discrepancy between scatter and other plotting methods, and the difficulty of making a nicely-formatted color bar. But it's really coming a long way since I started using it in ~2015, And in that time I've actually come to prefer it over both base R and ggplot2, which is what I started with originally.

I have much stronger complaints about Seaborn and he will never find me recommending it to anyone.


> The object-oriented API and its documentation have improved markedly over the years, I encourage you to give it a try. Most of the official documentation examples use it now. Yes, there are lots of examples out there using the pyplot interface still, but there is enough information on the OO API now that you can get by.

I agree. I used seaborn and matplotlib recently, and I was pleasantly surprised at both how much more consistent the API is and how much more comprehensive the documentation is. I dreaded using it at first but quickly became more accustomed.

> I have much stronger complaints about Seaborn and he will never find me recommending it to anyone.

Interesting, what are your pain points? For me, most of the time I start with seaborn, then modify the underlying matplotlib figure and axes for more customization; only rarely do I start with plt.subplots(). This gives me nice default styles while still allowing me to bend it to my will.


> start with seaborn, then modify the underlying matplotlib figure

Maybe this is the heart of my complaint: last I remember trying Seaborn, I found it hard to get access to the underlying Axes objects, and I also found it hard/impossible to add elements incrementally to an existing Axes. This might be a PEBCAK issue so I'm willing to try it again.


Understandable. A reason why I keep going back and forth between matplotlib and seaborn in the past is due to not knowing how to customize seaborn. Now that I know they are just regular matplotlib objects, it's very easy to mix and match them when seaborn is just too opinionated. You can read more here https://seaborn.pydata.org/tutorial/introduction.html#opinio...

Some examples I did recently: to set the layout engine of a seaborn plot

    g = sns.relplot(df, x="value", y="y", col="variable", col_wrap=3)
    g.figure.set_layout_engine("constrained")
    g.figure.set_size_inches(16, 12)
Set the subplot titles

    g = sns.displot(df, x="value", col="model")
    g.figure.set_size_inches(16, 8)
    for (ax, title) in zip(g.axes[0], titles):
        ax.set_title(f"Title: {title}")
Multiple seaborn plots with two lines, error band, and scatter placed onto existing matplotlib subplots and custom colors

  colors = sns.color_palette()
  fig, axs = plt.subplots(1, len(ys), sharey=True, figsize=(16, 6), constrained_layout=True)
  for (X, y, ax, title) in zip(Xs, ys, axs, titles):
    sns.lineplot(x=X, y=y, color=colors[0], ax=ax)
    sns.lineplot(x=X, y=y_predict, color=colors[1], ax=ax)
    ax.fill_between(X, y_predict - std, y_predict + std, color=colors[1], alpha=0.3)
    
    sns.scatterplot(x=X_train, y=y_train, alpha=0.7, color=colors[7], ax=ax)
    
    ax.set_title(title)
    ax.tick_params(rotation=0)
(You can also prepare the data in pandas the way seaborn like it and create faceted plot declaratively; the point is you can mix and match them freely and intuitively.)


Can I suggest changing the title here to the article title? "Best Python Chart Examples" It's a little less accusatory and I think a nicer representation of what it is


I've changed it now. (Submitted title was "Yes, Python and Matplotlib can make pretty charts").

(I took out "Best" though because that word doesn't matter and will provoke tedious objections about how no, these are not the best examples.)


I’m particularly proud of this[1] figure I made using matplotlib for my PhD thesis. And yes, this is a single mpl figure and those are all individual axes. You do not want to see the source code.

[1] https://ibb.co/TLLBg37


Actually I am interested in the code. Did you use TikZ?


Nope, just plain matplotlib. The code was huge mess written under insane pressure, but I’m sure I kept it somewhere in my backups.


I also thought it was made with TikZ (which is a compliment). I used TikZ for my thesis: https://jeroenjanssens.com/phd/ and same thing here, the code is a mess.


There are countless stunning charts available on the web, many of which are crafted using programming languages like R.

Tired of hearing doubts about matplotlib's capabilities, I've curated and translated a collection of impressive charts using Python and matplotlib.

Explore them all with detailed tutorials and reproducible code at:

https://python-graph-gallery.com/best-python-chart-examples/


What's weird is that I don't think I've ever heard any serious practitioner doubt its capabilities. The main complaints I hear are just annoyance with pyplot.


Went down this road recently and I ended up deciding to work with holoviz instead: https://holoviz.org/

Having some level of abstraction on top of matplotlib is necessary if you don’t want to spend time tweaking every little thing.


I recently started using Holoviews/Hvplot/Geoviews/Datashader for some big-ish data viz, and it's been mostly positive, but with some notable annoyances. The performance on big data is excellent. But I spent maybe two hours trying to figure out how to draw a damn regression line over my plot and I couldn't. Also hv.extension() seemed to break plain Matplotlib, whenever I tried to generate a plot after calling that function, I got an error about a non-interactive backend when trying to view a plot interactively, even when I selected an interactive backend explicitly.


That's the thing with wrapper libraries: amazing at the beginning, but quickly becoming frustrating.


For interactive charts, simple APIs, multi-language compatibility, and interactive apps (Dash) https://plotly.com/python/


Wow, these actually impressed me. I've made a few matplotlib charts myself, but typically R/ggplot is significantly nicer and easier.

My takeaway is that matplotlib may be a good alternative for specific cases where you need much more control.


I agree that R/ggplot is often more straightforward, but the main reason I (and most colleagues I've worked with) tend to use matplotlib is just a preference for python over R in general (to each their own though, that's not a debate I'm looking to start). If I'm doing data analysis in python already, it's a pain to export out just to plot in R. That said, it's definitely a steep learning curve, especially because it's something people usually learn on the fly instead of having any kind of formal instruction. I do think seaborn has really helped boost ease of entry wrt plotting in python at least.


These look fantastic. If there's one thing I'd really like to automate with AI instead of hand coding, though, it would be making charts!


See this: https://github.com/microsoft/lida

It isn't great but it works for simple stuff well enough.


There aren't so many polished matplotlib charts out there. So I'm not even sure if chatGPT would be good at it.


The article is so full of ads that it's hard to see what's what.


Wow, these examples are amazing!

I've used matplotlib in the past and was disappointed by the final look of the generated charts, nice to know there's hope and I can make them look good the next time I need it :D


this site has so to much ads.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: