r/Python • • Aug 16 '21

Discussion Anyone else despises Matplotlib?

Every time I need to use mpl for a project I die a little inside. The API feels like using a completely different language, I simply can't make a basic plot without having to re-google stuff as everything feels anti intuitive.

Plus, the output bothers me too. Interactive plots feel extremely awkward, and its just wonky

EDIT: Despises working with matplotlib*. I'm thankful such a powerful library exists, and I get that for scientific papers and stuff like that it's great, but damn isn't it painful to use

711 Upvotes

165 comments sorted by

View all comments

Show parent comment

2

u/the_guruji Aug 16 '21

Some could argue that

from matplotlib.figure import Figure

fig = Figure(figsize=figsize, dpi=dpi)
ax = fig.subplots(1, 1)

ax.plot(x, y)

is overkill compared to

import matplotlib.pyplot as plt
plt.plot(x, y)

I don't really see the need to use the first over the next for saving to disk cause you can just do plt.savfig and not use plt.show. Jupyter has %matplotlib inline by default I think nowadays, so plots show up inline. Are there any performance benefits, cause if so, damn, I should have been using this.

People probably recommend pyplot since that's the easiest to understand (as long as your requirements are simple enough).

2

u/DrShts Aug 16 '21

My simple example was just for demonstration purposes.

I think using plt.plot and similar are discouraged because it works by keeping a hidden global state that is modified every time you call plt.something. This is why in a setting beyond a simple one-off plot the OO interface of matplotlib is usually recommended. And for a library that uses matplotlib having a hidden global state is pretty bad anyway.

But what I actually wanted to point out was that even in articles / tutorials about using the OO interface of matplotlib (just look at the first results when searching for "matplotlib object oriented") the structure is always

fig, ax = plt.subplots()
ax.set_title(...)
ax.plot(...)

while it's really rare that matplotlib.figure.Figure is mentioned. This is strange to me because by using the OO way you're trying to shed all these unnecessary global state / GUI wrappers but then still use plt to create the figure (which calls the GUI wrappers) while there is a perfect alternative without that overhead in the form of matplotlib.figure.Figure.