Python Chart Examples
python-graph-gallery.com
python-graph-gallery.com
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 ...
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.
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.
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.
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.)(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.)
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/
Having some level of abstraction on top of matplotlib is necessary if you don’t want to spend time tweaking every little thing.
My takeaway is that matplotlib may be a good alternative for specific cases where you need much more control.
It isn't great but it works for simple stuff well enough.
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