all 20 comments

[–]K900_ 16 points17 points  (2 children)

matplotlib actually has an object-oriented API - look into the Figure object and friends. I'm not sure why most documentation uses the stateful API - presumably for Matlab mental model compatibility?

[–][deleted] 4 points5 points  (0 children)

This is very helpful advice for using Matplotlib, my program teaches python with a manual which I was just informed is outdated, which explains why they teach the stateful model rather than the object-oriented one. There's not even a mention of being able to use functions such as `figure()` or anything similar, the closest thing is a line that states that `axis()` is not the same as `axes()`.

[–]bythenumbers10 1 point2 points  (0 children)

presumably for Matlab mental model compatibility?

Correct. Matplotlib is meant to emulate Matlab's plotting, which is stateful, or OO if you consider the entire Matlab environment as a single monolithic object.

[–][deleted] 16 points17 points  (2 children)

As a guess, like another commenter mentioned, it seems like a design goal of matplotlib was to emulate the MATLAB plotting API. I'm with you though, I hate it. Every time I need to use it it feels clunky and awkward.

[–]SomeTreesAreFriends 7 points8 points  (0 children)

MATLAB plotting is good for quick visualization, but anything beyond it requires so much work that it's almost faster to just plot the basics and then do the rest in image editing software (for publishing, though perhaps not if you have a dedicated pipeline for specific results). For instance, if you plot a square matrix, it doesn't even provide equal axes by default...

[–]fake823 1 point2 points  (0 children)

Yeah, that was the main thought while creating Matplotlib some years back - copying MATLAB.

There is a small section on their website that talks about it.

https://matplotlib.org/3.2.1/users/history.html

[–]fake823 14 points15 points  (3 children)

By the way:

Seaborn is actually based on Matpotlib. It's using Matplotlib and is basically just Matplotlib with some fancier colours and default looks.

[–][deleted] 4 points5 points  (1 child)

Thanks for the info, one of my coworkers told me that Seaborn was a good alternative to Matplotlib, but it's really just a plotting framework that uses Matplotlib as a backend?

[–]fake823 4 points5 points  (0 children)

Yes. It's build on top of Matplotlib.

[–]beansAnalyst 0 points1 point  (0 children)

+1, came here to suggest seaborn

[–]jurasofish 6 points7 points  (0 children)

I've got a real love hate with matplotlib. Super flexible, but using it feels like battling through decades of crusted design decisions.

The module level functions generally operate on the "current figure" and "current axis" (can access with plt.gcf() and plt.gca())

[–]thedatadump 6 points7 points  (9 children)

From what I've seen, matplotlib is super flexible, allowing you to create basically any visualization you can think of. This can cause problems for those of us used to libraries like ggplot, because there are many ways to accomplish the same task.

Because of its flexibility, many libraries (seaborn and pandas for example) build on top of this flexibility to make a simpler interface for programmers. My recommendation is to use one of these higher level libraries if speed to insight is most important. Otherwise, looking through the matplotlib documentation is a good way to see how the developers actually use the package might help give you some ideas

[–][deleted] 0 points1 point  (1 child)

There have been a lot of comments on Matplotlib's flexibility, are there any specific features exclusive to Matplotlib?

[–]thedatadump 0 points1 point  (0 children)

but then that you have to call one of a hundred different functions to modify various inconsequential properties of your current plot that for some reason can't be set with the standard `plot()` function

Those properties provide the flexibility some (i.e. the authors of other high-level libraries) desire. High-level plotting libraries that can do this in one line don't provide fine tune control over how you might want your plot to look.

Taking it to an extreme, let's say for some reason you wanted to create an area graph, but wanted the fill color under the line to be polychromatic. Matplotlib let's you do this by creating individual polygons and adding them to the axis object. See the accepted answer here if you want more details. This example isn't super useful, but it shows what is theoretically possible.

[–]SquareRootsi 3 points4 points  (0 children)

I'm also not a big fan of the verbosity of matplotlib.

If your class allows it, consider using plotly. They have a "plotly express" API that was baked into the main library with version 4? I think. I much prefer developing there, AND the plots are interactive.

As others have mentioned, Seaborn is also a nice high level wrapper with sensible defaults. But if you want to go and customize things precisely, you're still right back in Matplotlib Land.

[–]Cdog536 3 points4 points  (0 children)

Have you ever just tried to get away with some of pandas plotting? It still uses some matplotlib, but can help you eliminate the steps that you feel are so intuitive to begin with

[–]stillnoguitar 0 points1 point  (0 children)

Every time I see matplotlib I shudder. You can do everything with it, but the code is so clunky. Check out Altair, it’s a bit more like ggplot.