r/Python • u/jachymb • Apr 21 '22
Discussion Unpopular opinion: Matplotlib is a bad library
I work with data using Python a lot. Sometimes, I need to do some visualizations. Sadly, matplotlib is the de-facto standard for visualization. The API of this library is a pain in the ass to work with. I know there are things like Seaborn which make the experience less shitty, but that's only a partial solution and isn't always easily available. Historically, it was built to imitate then-popular Matlab. But I don't like Matlab either and consider it's API and plotting capabilities very inferior to e.g. Wolfram Mathematica. Plus trying to port the already awkward Matlab API to Python made the whole thing double awkward, the whole library overall does not feel very Pythonic.
Please give a me better plotting libary that works seemlessly with Jupyter!
23
u/slowpush Apr 21 '22
Sounds like you’re using the state full api and not the object oriented api.
12
u/[deleted] Apr 21 '22
Please elaborate on the shortcomings of matplotlib with examples.
133
u/2_plus_2_is_chicken Apr 21 '22 edited Apr 21 '22
It depends on what you need to do.
Do you want an easy of the box way to just say "give me a scatter plot that's not ugly by default" then Seaborn or Plotly.
Do you want full, precise control of everything at the price of no hand holding on style (you're going to change all the defaults anyway so it doesn't matter they start as), then Matplotlib is probably your only choice.
If you want full control for web based/JavaScript adaptive figures, then Plotly. I've known some people who have tried to make Plotly work for static figures, but since that's not what it's made for it can be difficult, and you're probably better off with Matplotlib.
In the end it comes back to the eternal tradeoff between easy out of the box and full flexibility.
303
u/proof_required Apr 21 '22
It hurts even more if you come from ggplot world.
349
u/K900_ Apr 21 '22
I don't think it's particularly unpopular?
46
u/neuronet Apr 21 '22
My main complaint is why are there different method names in the oo api? Why set_title() in one mode and title() in the other? Why haven’t they just overloaded in both directions?
20
u/justneurostuff Apr 21 '22
I think practically all the mature figure plotting libraries in Python are built on matplotlib and require knowhow about matplotlib to extend. But plotly might be an exception?
66
u/[deleted] Apr 21 '22
How is Seaborn not "easily available"?
Unless your development environment is absurdly locked down, just install the plotting package of your choice (Seaborn, Plotly, Bokeh, whatever). And if it is that locked down, well, choosing a different library that you can't install won't help much.
119
u/UglyChihuahua Apr 21 '22
Plotly is the best IMO
33
u/mbussonn IPython/Jupyter dev Apr 21 '22
It also depends which part of matplotlib API are you using ? The old state full or the new object oriented one ? And what do you plot with it ?
There might be better libraries but it will depend a lot of what you are trying to plot.
23
u/tunisia3507 Apr 21 '22
Some of the worst most popular parts of the python ecosystem are bad because they were designed to be familiar to people coming from other languages with crappy design choices. The worst parts of numpy and matplotlib are there for MATLAB familiarity; pandas could be much better if it weren't for trying to suit R dataframes.
It is possible to use matplotlib in a way which makes it significantly better, but next to no tutorials or docs do so. But it's still not great.
30
u/[deleted] Apr 21 '22
People mentioned plotly, I find altair is great as well as it also has much less boilerplate than matplotlib.
I think more and more that matplotlib is not actually a bad library but rather has had a poor branding story. It's presented a the de facto plotting lib for Python but it exposes a ton of internals and has so many possible options that it quickly becomes overwhelming.
It would probably benefit from marketing itself as the "low-level" library for plotting when you want precise control on your plots but redirect people to seaborn/plotly/altair, when you want something quick and easy with decent defaults.
21
u/ponnhide Apr 21 '22 edited Apr 21 '22
I think plotnine is one solution. It is implemented based on matplotlib, but it provides an almost complete ggplot syntax for matplotlib. The other solution is a next-generation seaborn interface. It is also built on matplotlib and is still in progress; however, the API would be really useful!
And I have personally developed a few libraries to solve the complex syntax of matplotlib. As an example, patchworkllib allows dynamic subplot layout on Jupyter-lab. Maybe the library can support handling matplotlib and seaborn plots.
19
u/GasimGasimzada Apr 21 '22
Matplotlib's API has this interesting phenomena where even if you learn it, you still are not sure if it is going to work.
6
u/[deleted] Apr 21 '22 edited Apr 21 '22
I'm quite happy with the HoloViz stack so far (although I really only use HoloViews and HvPlot. You have a *mostly unified API for using either matplotlib, plotly or bokeh as the backend (default is bokeh so you have interactive options), and can use declarative API without too much step-by-step graph building. It interfaces nicely with many data structures, although it is not as easy to work with bare data (naked numpy arrays outside dataframes) if you specifically want imperative plotting. Its API is similar to ggplot where you declare plot elements and simply "add" (compose) or "multiply" (overlay) them together. There is a convenient helper object to let you use tab prompting to see what configuration options are available for a particular object. It also supports datashader for plotting very large datasets without lagging. The default API still has some of boilerplate, but the hvplot API "replaces" pandas's default matplotlib API and provides a quick and dirty way to instantly visualise your data.
I have not loaded up matplotlib for ages. This is the library to go if you want nice plots from bokeh / plotly but feel their APIs are too arbitrary.
There is one major downside though. The library is still built on top of three different plotting backends, so if you really want to customise your plot, you WILL need to know the differences between how these libraries handle their objects.
11
u/pwnersaurus Apr 21 '22
The real unpopular opinion is that plotting in Matlab is a way better experience 😅
5
u/GuybrushLightman Apr 21 '22
laughs in gnuplot
4
u/HrOlympios Apr 21 '22
Matplotlib's documentation is frustratingly bad compared to most python libraries, but I still use it often due to the high volume of examples you can find on Google.
I've heard good things about Seaborn but my first encounter with it was checking the code of one of the sloppiest data scientists I've ever had the displeasure of working with, so now I have an irrational hatred of the thing
9
u/iyousef_46 Apr 21 '22
I don't work with data so I can't say what's good and what isn't, but you might want to look at Plotly.
6
u/DJStuey Apr 21 '22
Big fan of plotly.
3
u/likethevegetable Apr 21 '22
Depends on what you're plotting for. If you need fine-grain control for report ready plots (that match your LaTeX document), I think it's great. I don't see what's un-Python about it... You make a figure object, and within that figure there are axes. You then modify attributes of those objects... Once you learn to stop using plt.plot() and start ax.plot(), it makes more sense.
3
u/BK201_Saiyan Apr 21 '22
"Anyone who says money doesn't buy happiness doesn't know where to shop."
These two resources usually solve most of my problems with matplotlib, combined with seaborn, of course.
3
u/O2XXX Apr 21 '22
Plotnine is basically a GGPlot clone (probably the most popular visualization API for the R language). It's a much more logical than Matplotlib and uses a layered approach to add different function to your visualization.
https://plotnine.readthedocs.io/en/stable/index.html
3
u/AppropriateSeesaw1 Apr 21 '22
My pet peeve is instead of the intuitive point1,point2 you can only pass [x1,x2],[y1,y2] to .plot
3
u/broken_symlink Apr 21 '22
Over the past 6 years I've used plotly, bokeh, and pyqtgraph all for non-trivial scientific applications. I've also used dash and holoviews on top of plotly and bokeh. My go to library for day to day use is plotly, for streaming data I've gotten comfortable with holoviews and for high performance pyqtgraph.
5
u/1percentof2 Apr 21 '22
Have you tried pyqtgraph? It's quite a bit more fancy.
1
u/[deleted] Apr 21 '22
What API would you suggest as alternative? I think Seaborn is much more complicated than Matplotlib.
1
u/skytomorrownow Apr 21 '22 edited Apr 21 '22
I see Matplotlib as an extremist: for quick, down and dirty plots as you work, and for really complicated stuff, where you need to tap into the object-oriented version of Matplotlib, it's great. If you want something easy, more modern, animated, and they have something close to what you need as a template, then plotly or seaborn make more sense (and look better).
2
u/girlwithasquirrel Apr 21 '22
I think maybe you don't like the interface it provides. As a library it's great and highly customizable.
2
u/YetAnotherDaveAgain Apr 21 '22
I agree insofar as the function calls change when using axes vs figure plots. I do have to go to the api often to check if it's set_title or title("title") etc etc. So if they could go back and restructure some things that would be so great.
But it's also just so nice to be able to change any element of your plot at will! So is it a good library? Well it's certainly insanely powerful. But it is definitely clunky. So I'd say it's good, not excellent, but still indispensable.
2
u/billFoldDog Apr 22 '22
I'm in the same boat. Here are some thoughts:
2
u/stay_janley Apr 22 '22
Whole lotta whining in this thread about things that are ultimately very aesthetic and easy to overcome or get used to.
Matplotlib is a very powerful library. It has shortcomings. But they are much deeper, complex problems than surface-level complaints about doing the very basics like labelling a plot.
2
u/antennarius May 05 '22
Seaborn is so much worse if you want to go beyond the defaults
2
u/ShibaLeone Jul 06 '22
Matplotlib is what happens when Java programmers write the core api, then python programmers come in and try to make it more ‘pythonic’, and everyone is afraid to make a breaking api change so every legacy call config is supported at the expense of you knowing where anything is.
3
u/wewbull Apr 21 '22
The clue is in the name. It started by trying to clone MATLAB.
4
u/Federal-Ambassador30 Apr 21 '22
matplotlib is actually really good, you just need to learn to use it properly. Have seen some crazy visualisations made using it.
But if you are looking for a decent alternative look at the seaborn library :)
2
u/[deleted] Apr 21 '22
I get that. I tried to work with Python for Explanatory Data Analysis including some quick visualizations and data manipulation but I realized I was spending way too much time fighting with Pandas and Matplotlib to work for me rather than the other way around.
The fact of matter is that R's ecosystem (tidyverse/tidymodels) is way more consistent and intuitive for at least the EDA aspect of data science.
2
u/[deleted] Apr 21 '22
What is matlab?
2
u/bjourne-ml Apr 21 '22
Matplotlib is very powerful, but yes, the API is cluttered and confusing.
Write one yourself!
1
u/WORKERS_UNITE_NOW Apr 21 '22
Seaborn is built on matplotlib and isnt shit! I love it
1
u/[deleted] Apr 21 '22
Yeah, I'm not a fan either. I still use gnuplot.
1
u/shinitakunai Apr 21 '22
When I need visualizations I just use Splunk
1
u/rhd9b Apr 21 '22
My team uses Plotly, which made my life more difficult as a matlab convert since all the Python courses teach matplotlib. Seems to generally get the job done, but I’m not always crazy about how much code is needed to get there.
1
u/greasyhobolo Apr 21 '22
I am with you. I find the api very unintuitive/unpythonic. My vote for best viz library would be bokeh. It comes with a fairly steep learning learning curve, but really it's only steep because of the level of customization/interactivity it can offer. Basic plotting is actually very intuitive/straightforward but the major hurdle is the sheer number of models/classes you have access to... it can really convolute things when learning and it may *seem* too complicated (but it really isn't!). The bokeh discourse page is pretty helpful too.
1
u/runawayasfastasucan Apr 21 '22
Add on trying to use basemap and you got a nightmare. Having the right versions and getting it to work with conda is far to cumbersome.
1
u/Northzen Apr 21 '22
Can actually describe for me or point out where to look for if I need:
My problem is that I would like to have a figure which gets updated when new data comes, but all I found just some spaghetti on StackOverflow.
1
u/DemonLoam Apr 21 '22
Try plotly, I've been enjoying it. A bit of a change but I think it ends up being more usable.
1
u/marshmallow_peep Apr 21 '22
Use the objects, not the Matlab interface. Seaborn works great if you just want a bar chart or whatever. I've made a bunch of complex plots in matplotlib that are impossible in ggplot or Altair, matplotlib can really get weird if you need it.
The only library I know of more capable than matplotlib is D3; consequently it's even harder to fully understand.
1
u/jawnlerdoe Apr 21 '22
As someone just learning python (and programming in gen) it’s these takes that I come here for
1
u/AUGotMyGold Apr 21 '22
I used to think this too, and I'll admit MPL's API can be a little confusing, but once I took the time to really learn what's going on under the hood, I stopped reaching for other tools like Seaborn as often.
It all depends on what your use case is. Need something quick-and-dirty that looks OK? There are other, better-suited choices.
Do you need to finely control everything about the look and feel of your viz in (sometimes excruciating) detail? MPL is hard to beat.
1
u/AddSugarForSparks Apr 21 '22 edited Apr 21 '22
You can plot with
pandas.All visual libraries are complicated, anyway. Just try plotting with d3.js if you want to kill off an afternoon/weekend. Lol
1
u/BayesDays Apr 21 '22
Plotly
1
u/[deleted] Apr 21 '22
yes
1
u/BertShirt Apr 21 '22
Matplotlib "supports" its own version of the Matlab plotting interface, but the way matplotlib is meant to be used is through the object-oriented interface. I highly recommend you learn the object orientated approach as it is quite nice and allows for some seriously detailed tinkering. That being said, I understand most people might not want to tinker as much as I do, and respect that different people have different preferences.
1
u/riftwave77 Apr 21 '22
-Sung to the tune of Leroy Brown-
Its bad (bad), bad (bad) MATPLOTLIB
worstest module to get imported!
Badder than recursive function calls
and meaner than a core dump log
1
u/wineblood Apr 21 '22
I haven't touched matplotlib in years, but I remember writing my own adapter for some functions because their interface is ridiculous.
1
u/teristam Apr 21 '22
I am using seaborn extensively. Working wonders.
1
u/metaTaco Apr 21 '22
I made plots using CERN ROOT for a long time so Matplotlib is pretty damn good by comparison.
1
u/IlliterateJedi Apr 21 '22 edited Apr 21 '22
What an incredibly brave stance.
Also I could have sworn Matplotlib (or someone) teased a more ggplot-esque way of doing things that's being developed, but for the life of me I can't find it anywhere online.
Edit:
j/k it's Seaborn that's redoing their API
1
u/sweeetscience Apr 21 '22
I like Plotly. It’s API can be a PITA sometimes, but the interactive results from a simple fig.show() can be pretty cool. Also has 3D modeling capabilities.
1
u/Zeroflops Apr 21 '22
Matplotlib has two interfaces. The OOP interface and the Matlab interface. What really screws ppl up is when they try to use the two interchangeably or they see one set of documents detailing one and another showing another.
Learn the OOP interface and avoid the matlab interface and it will make more sense and easier to use.
1
u/bitbyt3bit Apr 21 '22
Seaborn is the goat
1
u/[deleted] Apr 21 '22
I wonder if you ever tried ReportLab? It’s known for making pdf but it has a full charting library and is heavily used by multiple financial entities. It’s more feature rich than matplotlib but I can’t decide if the API is less or more friendly, it’s very different!
1
u/Rajendran-Sp Apr 21 '22
It's creepyy
1
u/zelphirkaltstahl Apr 21 '22
I would say it does not have the best API evaaar, but it is quite useful for generating high quality plots for all kinds of things. It is very versatile. The procedural interface is trash though. Basically no one sane uses that. I would like a more declarative interface.
1
u/mibuchiha-007 Apr 21 '22
Unpopular?
1
u/edsuom Apr 21 '22
I got so frustrated I wrote a package to make it easier, yampex which stands for “Yet Another Matplotlib Extension.” It took some effort but I’m really glad to have it: https://pypi.org/project/yampex/
1
u/[deleted] Apr 21 '22
It's annoying to learn but pretty straightforward after you understand the design. Everything is layers.
1
u/therc7 Apr 21 '22
plotly supremacy 🤲
1
u/EarthGoddessDude Apr 21 '22
I prefer Plots.jl (or it’s close cousin, StatsPlots.jl). Sometimes I dabble in Makie 😉
1
u/Eze-Wong Apr 21 '22
Ha! I just responded to a tiktok today about how to solve matplotlib binning issues and I was like "Dont use matplotlib" . Plotly gang where you at? Ya ya...
1
u/_Will_Rice Apr 21 '22
I honestly thought this was the most popular opinion.
1
u/cantdecideone Apr 21 '22
I agree it can be fiddly. A lot of pain can be solved by setting up plt.rcParams which let's you change and edit any of the default parameters to what you want. Do this at start of script and then much less editing on each individual plot
1
u/DrPRNDS Apr 21 '22
Have you tried plotly? I've found it to be so much easier to use (and so much better looking) than Matplotlib and Seaborn. I frequently make basic plots with a single line when using plotly express in a jupyter notebook.
1
u/nraw Apr 21 '22
Plotly or Altair.
Yours is not an unpopular opinion, the only reason I've seen people use matplotlib was because they just didn't know any better and courses keep teaching that thing for some reason..
1
u/[deleted] Apr 21 '22
Ye its shit
1
u/stay_janley Apr 22 '22
if you have problems with it, submit a PR
1
u/PM_ME_YOUR_MECH Apr 22 '22
As someone who had to do a zoomed inset on a basemap a few days ago... I agree. The calculations generating the data were somehow the simplest part of that code
1
u/G4L1C Apr 22 '22
Matplotlib imo eas bad written, it's over complicated, had bad name choices, and is internally inconsistent in jts design. Ggplo2 or plotnine is by far better.
1
u/Whitishcube Apr 22 '22
HOLOVIEWS. I had much the same feeling towards matplotlib. Give this a try. https://holoviews.org/
1
u/hkanything Apr 22 '22
Plotly, Seaborn and Vega
1
u/pysk00l Apr 22 '22
I hate matplotlib too, so much so I've looked into Javascript plotting just to avoid matplotlib.
Sure there are Bokeh seaborn etc, but I dont find them very intuitive. Just compare with any R or Javascript plotting library, and you see how much the Python one if confusing.
1
u/Illustrious_Ad_1456 Apr 29 '22
Have you ever tried plotly.express? Minimal effort for amazing graphs
1
u/[deleted] May 01 '22
Try altair. It follows the grammar of graphics
1
u/TehBeege Apr 21 '22
Sounds like a PR is in your future, friend
1
u/[deleted] Apr 21 '22
These days for anything that's not completely basic I believe it's mostly Plotly.