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
278
u/vlizana Aug 16 '21 edited Aug 16 '21
As it has already been mentioned, Matplotlib inherits its interface from Matlab, as it is also the case with many scientific libraries like numpy and scipy. But also when it comes to visualization you have low level and high level libraries.
Matplotlib is a low level library in the sense that you go around modifying the diferent individual elements of the visualization, you have a lot of granularity but it might take a lot of code to get what you want, and this also makes modifications more troublesome. That being said, Matplotlib nowadays has fallen way behind on this category in terms of features and ease of use to modern libraries like Plotly and Bokeh, and the majority of people using it is just out of inertia (pretty much like Matlab).
High level visualization libraries offer semantic interfaces that are often implementations of some visualization theory, like the so called grammar of graphics that is behind libraries like Altair and Plotnine (which is to many the heir of ggplot). These libraries are generally more intuitive to people with a theoretical/mathematical background while low level are often more intuitive to programmers or people with a more technical background.
I don't despise Matplotlib, I just don't use it anymore as there's no real reason to. Keep looking until you find the right library for you.
Edit: Glad to see a lot of people recommending Plotly, I didn't want to introduce any bias but it is also my library of choice, so it's great to see the community growing.
66
u/loopyroberts Aug 16 '21
That's probably because it's trying to replicate MATLAB plotting and was a way to transition away from MATLAB to Python for all the engineers and scientists that aren't programmers.
I know plenty of people that will never give up MATLAB even though everything they use it for can be accomplished (probably more easily) in Python.
60
u/the_guruji Aug 16 '21
Loaded questions like this are only going to attract people with nothing good to say about Matplotlib, but OK, here's my take:
The problem with Matplotlib is that you can do whatever you want with it, and it tries to let you, so you get this huge library most of which you will never even touch. Altair and Seaborn assumes things about your plots, and for those kinds of plots, they are amazing. So much plot with so little code. But you can't really go beyond that. If you need anything niche, you'll have to fall back to Matplotlib and packages built on it.
Pet-peeves:
**kwargsand say go look up this random thing that we inherit from. It would be so much easier to know immediately what to use.linestylevslinestyles, but atleast these changes are in places that make some sense.People trying to use Matplotlib for interactive graphs will obviously face issues, the original API itself is crap (MATLAB) for normal plots. But I continue to use Matplotlib for a few reasons:
Honestly, I don't think the API needs to be re-written. The gets and sets I can live with as long is it is used everywhere. The only major problem IMO is the lack of documentation, good practices and clarity on how to change specific things (how do I add an extra special tick on the X-axis, how do I put in images, say country flags, on the xticklabels etc).
OP, if you're using Matplotlib for static plots and you still have this issue, why don't you put up the questions themselves here or on r/learnpython, maybe there might be a better way to do whatever it is you want to do. If you want you can DM me also, I'll try to help as much as I can (but I'm not ultra-active on reddit either).
26
u/GiusWestside Aug 16 '21 edited Aug 16 '21
If you suggest to use Altair instead of matplotlib you don't have the faintest idea of what is matplotlib and what is Altair.
Being good at matplotlib means that you can be a litteral GOD when creating 2D graphs. And if you combine it with seaborn you can do data science plots in a second. If you need anything else then you might need Altair or plotly to produce interactive graphs or 3D graphs.
A lot of you compare matplotlib to ggplot and it's an error. Ggplot follows the grammar of graphics and it's incredibly limited in what it can do. You want the ggplot feel? Then in Python there is plotnine
9
u/[deleted] Aug 16 '21
You are not alone. Sometimes if the visualisation is more complex than a line graph I switch to R and use ggplot. That feels much more natural.
9
u/pymae Python books Aug 16 '21
I wrote a book about visualizations in Matplotlib, Seaborn, and Plotly because I was tired of having to look up everything for Matplotlib.
I think Plotly is a great alternative with solid high-level pre made functions, and low-level customization like Matplotlib but without the MATLAB syntax.
31
u/crawl_dht Aug 16 '21
This is why I use seaborn.
15
u/scraper01 Aug 16 '21 edited Aug 16 '21
Feel the same way, it's object orientation is wonky. Out of my libraries toolbox this is the one i've never been able to memorize anything but the most trivial stuff. Seaborn is a bit better.
26
u/bbateman2011 Aug 16 '21
I thought it was weird after using ggplot in R, but now I generally prefer it. The syntax is Python seems inconsistent to me but I got used to it.
5
u/DrShts Aug 16 '21 edited Aug 16 '21
After years of using
matplotlibI came to realise that I should pretty much never doimport matploblib.pyplot as pltas pyplot will always activate whatever GUI backend is configured (Tk, Qt, ...)So I've been experimenting with replacing code like this
by
Why do people recommend using
pyplotmost of the time? Seems like it's really only useful if you want to display the plot in a new window. For saving a plot to disk or for working in jupyter notebooks it's a total overkill.4
u/testuser73847 Aug 16 '21
So after 4 years and 3000+ hours, I think I’m comfortable enough with it that I don’t hate it. I mostly use it in conjunction with Seaborn, but my knowledge of the former helps me extensively customise figures for publications.
That being said I’m open to hearing about alternatives. I didn’t use plotly so much 4 years ago because the documentation was geared towards their hosted version—is that still the case?
Also in contrast I find ggplot frustratingly purist at times—sometimes I want to add an element, completely independent of the data, for annotation or because my boss has requested it. I don’t always find visualisation to be an abstraction of data to be a helpful mentality.
9
u/iiMoe Aug 16 '21
Plotly all the way
10
u/kingscolor Aug 16 '21
Can’t stand it.
It was the opposite of intuitive to me when I first started with Python. I tried a few others, Bokeh, Seaborn, etc. which also turned out to be unintuitive or feature sparse. I eventually landed on Plotly and will never go back.
Modern, feature-rich interactive plots with an intuitive code base. It’s written in JS, I believe, but the python port is highly documented and truly a powerful library. Most importantly, I love the aesthetic.
12
u/nraw Aug 16 '21
I have no idea why people keep using that, when there's better alternatives out there like plotly and Altair.
It feels like everyone coming from R is learning python with the same course.
5
u/32sthide Aug 16 '21
just use altair: https://altair-viz.github.io/
5
u/sigbhu Aug 16 '21
Huh, matplotlib is the only package in python whose syntax is reasonable for me
2
u/[deleted] Aug 16 '21
Whenever I debug, the matplotlib window gets frozen and that's the part I hate the most. Have you guys experienced this? Any possible solutions to this issue?
2
u/TinyCuteGorilla Aug 16 '21
YES. That's why it's easier to use Plotly or some other library with a high-level API for simple plotting. But here's the thing, if you need something really custom, idk like you want to draw a football field than put up graphs on it, you need something low-level that matplotlib provides.
2
u/[deleted] Aug 16 '21
Agree 100%
2
u/[deleted] Aug 16 '21
Seriously, after using R’s ggplot I think all other graphics APIs have a huge room for improvement.
2
u/rightheart Aug 16 '21
As Matplotlib exists since 2002, at least the documentation had a chance to develop and is pretty detailed. Nicer aestetics are possible using Seaborn as a style. Otherwise Plotly is worth a try, and is quite useful if you want to make interactive dashboards in Dash at some point.
2
u/PizzaInSoup Aug 16 '21
tbh I like it because it's very configurable and customizable, I actually want to have control over every minute detail and I tend to make some crazy plots with lots of information on there
2
u/NewDateline Aug 16 '21
Check out https://plotnine.readthedocs.io/en/stable/ it sound like something you will like
3
u/[deleted] Aug 16 '21
I use Bokeh. I feel like I am cheating.
1
u/idetectanerd Aug 16 '21
Woah. I haven’t use it yet but I’m glad that I’m matlab trained during University as many here say it’s more like matlab.
1
u/james_pic Aug 16 '21
Maybe I don't have the same complex requirements as others, but I usually just use PyGAL and call it a day.
1
u/maximeridius Aug 16 '21
I had similar feelings about all the R and Python data vis libraries and ended up learning Javascript, which a lot of the libraries are based on (not matlpotlib though), so now I can actually use a native api, I've even written my own charting library. I would require some effort to use JS interactively in something like Jupyter, but for dashboarding I find it's much easier. Things like R Shiny and Python equivalents are just confusing abstractions over HTML/CSS/JS, which is unnecessary since they are easy to learn.
1
u/[deleted] Aug 16 '21
Not so much as pandas, but it's my number two stackoverflower.
1
u/Sugarox53 Aug 16 '21
I took one look at they shut and had an “I can’t be fucked moment”.
Can someone explain how to use it in simple examples?
1
u/planet36 Aug 16 '21
I despise it when the devs deprecate the API parameters I use and then don't tell how to update my code to be compliant.
1
u/Starvexx Aug 16 '21
Maybe
1
u/sofiyach Aug 16 '21
The most stressing part of my thesis presentation was plotting the data with Matplotlib :D
P.S I was doing Extended Kalman Filter on group UAV flights
1
u/Chivalric_75 Aug 16 '21
Try to plot something with Julia and you will regain some lost love for Matplotlib.
1
u/MetalOrganicKneeJerk Aug 16 '21
I always thought matplotlib was better for publications. Has this changed?
1
u/lmlodzian Aug 16 '21
You may like Seaborn. It's a higher level API built on top of matplotlib. I recommend the Kaggle micro-course on data visualisation that teaches you the most important Seaborn functions in 3 hours including practice exercises.
1
u/Jhchimaira14 Aug 16 '21
Have you tried Dear PyGui? https://github.com/hoffstadt/DearPyGui
1
u/PsychoNAWT Aug 16 '21
Not as much as I hate D3
1
u/Veggies-are-okay Aug 16 '21
I really only use MPL for personal EDA, and even that is out of inertia. As others have mentioned, plotless seems to be the way to go in terms of manageable and aesthetically pleasing visuals.
1
u/YetAnotherDaveAgain Aug 16 '21
I have a pretty balanced opinion. Matplotlib is so flexible that it can be great for making weird and novel plots. Great for scientists! But it also takes a lot of work to make those. Also, it blows my mind that the syntax is so different when using plt.<plot something> and ax.<plot something>
I also enjoy seaborn and plotly, but sometimes I'm not in the mood to get my data into a dataframe of the right shape.
Anyway, my point is that they are all useful and if you get modestly fluent in a couple you can decide on the fly what's best for the task.
1
u/redrumsir Aug 16 '21
Despise? No. I'm glad somebody else did all the hard work for me. It's a useful tool.
1
u/ogrinfo Aug 16 '21
+1. I’m a professional Python developer but every time I need to anything with matplotlib I have to Google it. It’s so unintuitive (unlike other Python libraries at least) and the examples in the docs are generally not helpful. Some useful hints here though, so thanks all! Maybe I’ll give plotly a go.
1
u/dogs_like_me Aug 16 '21
There are lots of great tools with much more convenient APIs built on top of lower-level visualization libraries like matplotlib. Here's a project I like a lot that lets you use lots of different lower-level libraries to power your visualization from a common higher level API: https://pyviz.org/
I particularly like combining hvPlot and Panel with Voila to build simple visualization apps
1
u/AlbertPassy Aug 16 '21
It's all there in the name. It'd meant for old farts like me who came to python through Matlab.
1
u/LiarsEverywhere Aug 16 '21
I have conflicted feelings about it. Yeah, it's ugly. And syntax is complicated. Sometimes it can be infuriating. On the other hand, there was never something I couldn't do with it. And that's a big plus, because I use it for scientific publications. Precision and flexibility outweighs pretty for me. I've never needed interactive stuff etc.
So, I used to use SPSS (or even Excel) to plot simple things and get a general idea of how things would look, and went back to matplotlib to generate the final plots with precise code. I got a free Tableau license recently, and I'm trying to learn it. Maybe it can be the best of both worlds, I don't know. It lets me do things like stacked bars, which is often tricky with simpler tools.
1
u/O_X_E_Y Aug 16 '21
Don't mind it at all honestly, I can usually get what I want pretty easily
1
u/DevWolf59 Aug 16 '21
mpl is hot garbage especially if ur using numpy as well
1
u/fried_green_baloney Aug 16 '21
Lots of packages sit on top of Matplotlib. Seaborn is one. Simplifies quite a bit.
1
u/PM_UR_REBUTTAL Aug 17 '21
At a matlab user, I hated it before it was cool.
1
u/xristiano Aug 17 '21
Altair has a great API, plotly works in production
1
u/himalayanSpider Aug 17 '21
Coming to Python from Matlab , I loved Matplotlib. It was flawless transition to Python
1
u/Eiterbeutel Aug 17 '21
Try plotnine. It's a python copy of R's ggplot.
1
u/chrisxfire Aug 17 '21
this.
whenever I have a choice, I choose plotly. whenever I have to work with marplotlib, I die a little inside.
1
u/gagarin_kid Aug 17 '21
I personally heavily use seaborn with small matplotlib.axes. Axes-level adaptations. In comparison to plotly I like the JSON-free interface and high quality images.
For more explorative working mode on time series or other unaggregated data bokeh/plotly seems to be a better fit.
1
u/CrambleSquash https://github.com/0Hughman0 Aug 17 '21
plt.annotation('label', xy=(0, 0), xytext=(10, 10), arrowprops=dict(arrowstyle='->'), horizontalalignment='center')horizontalalignment='center'I moan, but I actually use matplotlib on an almost daily basis. You can basically use it to make any 2D graph you can think of (accept split axes!).
1
u/Rough-Membership8517 Aug 17 '21
Absolutely
1
u/Darwinmate Aug 16 '21
Stop using it. There are alternatives like seaborne and toyplot.
1
u/jjcollier Aug 16 '21
Hate it. The one time I tried to use it I spent weeks trying to get a simple 2D scatter plot, the kind I could get in Excel in 30 seconds with 6 selections.
When I finally went to a forum to get help for putting the axes in the center of the graph, because, you know, negative numbers exist, someone posted an example where they'd eyeballed the number of pixels in the graph and then manually translated the axis to somewhere near the middle. Everyone else on the forum acted like that was a perfectly sane solution and that I was being unreasonable for expecting some kind of normal "divide the width of the plot by two and put the axis there" option like every other graphing program can do. It was like trying to have a conversation with a cult. Fucking ENIAC could divide a number by two, but Matploblib can't?
1
u/eveninghighlight Aug 16 '21
I love matplotlib, I like how there's both the global variable and object oriented approaches to plotting stuff
1
u/BDube_Lensman Aug 16 '21
Your post title and the majority of your post (EDIT notwithstanding) would be really effective at making people not want to contribute to or maintain matplotlib. But many of the "alternatives" that just offer a higher level API are actually built on top of mpl. Dis-encouraging people from working on a very foundational library in python is probably going to have negative outcomes for you, even if your "endgame" isn't using it directly. If the output isn't html/svg/js, there's about a 99% probability it was ultimately rendered by mpl (well, Cairo or something, but through mpl).
1
u/vanatteveldt Aug 16 '21
ggplot for the win :)
0
u/siddsp Aug 16 '21
I don't hate Matplotlib, but it is extremely limited in terms of functionality. Despite trying to read the docs, I still haven't found a way to make actual graphs be interactive, that is, having features like a cursor and hovering to show x and y values for a graph function.
Plotly has that, but unfortunately, Plotly graphs cannot be embedded in Tkinter.
0
u/GoofAckYoorsElf Aug 16 '21
Yeah. There are way better plotting libs, if you ask me.
0
u/anax4096 Aug 16 '21
It's a decent library showing its age, but when it pops up in the output of a `pip install` I start to worry.
"ok, here we go"
43
u/[deleted] Aug 16 '21 edited Aug 16 '21
[removed] — view removed comment