Hacker News new | past | comments | ask | show | jobs | submit login
What's new in Matplotlib 3.7 (matplotlib.org)
167 points by todsacerdoti on Feb 15, 2023 | hide | past | favorite | 42 comments



Matplotlib is still by far the most complicated library I have used in my fairly long career (10+ years). I worked with pieces of software for molecular modeling with millions of lines of code that were easier to understand that matplotlib.

It's very powerful, I give them that, if you can think of a figure, it can do it. But you will probably spend an evening trying to make the labels not touch each other.


Just in case anyone hasn't tried it before, the "tight_layout" keyword in matplotlib is a game changer. Instantiating a figure via e.g. "fig, axs = plt.subplots(1, 2, figsize=(6, 3), tight_layout=True)" automatically arranges the labels and subplot boundaries etc to not bump into each other. It doesn't work 100% of the time, but it usually does a pretty good job and has honestly saved me a ton of plotting headaches.


In my experience, `tight_layout` tends to work best after all objects have been drawn:

  fig, axs = plt.subplots(ncols=2, figsize=(6, 3))
  fig.suptitle('Two Made-Up Plots')
  axs[0].plot(np.sinc(np.linspace(-5, 5)))
  axs[1].imshow(np.random.normal(size=(10, 10)))
  fig.tight_layout()  # works really well here!
More here: https://matplotlib.org/stable/tutorials/intermediate/tight_l...

You can also use `constrained_layout`, but in my experience it often doesn't work as well (e.g., the plots end up looking too cluttered for my taste), and also, `constrained_layout` can be noticeably slower (e.g., if you're drawing a large number of complex plots): https://matplotlib.org/stable/tutorials/intermediate/constra...


Please don't recommended right layout anymore. There is a new, much better layouting engine since a few release you can get with `layout="constrained"`


If tight_layout doesnt do the job, adding a ‘bbox_inches = tight’ may do it. I dont know why though.


10 years ago I loathed doing plots in Matplotlib.

When Plotly offline version matured (circa v 4) I moved over.

Lately, I've found that matplotlib is just fine for quick exploration when combined with Copilot prompts.

Instead of spending 20 minutes staring at matplotlib documentation, I can be done in 2 minutes with a prompt or two.

# my prompt can be something like get me horizontal bar chart of columns "foo" and "bar" from current dataframe. Rotate x axis ticks 45 degrees. Add y grid every 10 values. Use log scale for y. Add arrows with values for maximum values. Use emojis for markers.

# result might require tiny bit of adjustment but will basically be there. # Full disclosure: I haven't tried the emoji prompt...


I hear you. I've been using matplotlib for about as long, and as a python first person still find the API baffling. I avoid it as far as I can, Seaborn where possible. I've been trying out Altair but unfortunately the figure types are relatively restricted, though the figure logic is a relative joy to use.

These days, rather than struggling with the mess that is Python plotting, I drop into rpy2 and make the figure in R (so much easier and unusual figure types are covered).


It's baffling because it combines a Matlab style API with a Pythonic one using the same functions. If you know which is which it makes more sense.


I know that the matplotlib API combines two distinct styles, but it is little comfort when I'm struggling to wrangle a plot. To this day, I have to look up the API to do anything even slightly non-standard. Contrast with ggplot in R where it's a lot more straightforward. I've tried 'language of graphics' implementations in Python like Plotnine but they're just subtlety different enough from the ggplot implementation in ways that are annoying.


ggplot2 is great until you want to do something slightly off-piste, then it's absolute madness, it has it's own OOP system because R didn't have enough of them already.


I think one of the major advantages of ggplot2 is the ecosystem - often somebody has already implemented the figure type I'm looking for as a ggplot2 extension. This is not to say that ggplot2 or R are capable of plots that can't be achieved in python or matplotlib. Unfortunately for the type of plots I'm usually trying to make, it's more clearly laid out in R rather than python. Python tends to offer me far more friction. For example, I'm unaware of any python package that simplifies creating annotated heatmaps - like pheatmap or complexheatmap can produce. I'm sure matplotlib can make them with enough skill, but it's not my forte. I find it easier to drop down into R and just call pheatmap.


Look at gnuplotlib if you want another option. It's nicer in a whole lot of ways.


One slightly redeeming feature about Matplotlib, in comparison to using matlab/octave plots or a spreadsheet, is that it is such a pain in the ass to use that I end up writing a full script rather than trying to manually tweak. So at least it is easy to reproduce my figures.

This probably looks like grumbling or something, but it is sincerely a great help.


Github Copilot manages to generate the boilerplate for matplotlib. The result can be evaluated easily just by looking at the plot. The code is mediocre at best but it allows to produce the desired plots quickly.


Arcane syntax from matlab. And too many ways to do the same thing. Pretty much violates the zen of python.


My experience has been the same. I always end up resorting to TiKZ/PGFPlots, which unironically has a steeper learning curve but rarely negatively surprises you once you get the hang of it.


complexity you can wrap, seaborn style, trading some flexibility for an easier API.

the real "competition" is, I believe, d3.js and visualization grammars built on top of SVG and d3 (vega, vega-lite) that allow amazing interactivity for non-technical users.

you can integrate matplotlib nicely with, e.g., django to deliver server-rendered visuals, but its all static. this relegates matplotlib to exploratory modes (within an IDE or notebook) and production pipelines.

this all might change with pyscript and a browser backend, remains to be seen.


Thanks for the link to Vega. That library/api is very impressive.


Plotly is real competition and does html interactive stuff. I found it quite easy to use. I still prefer matplotlib though.


plotly offers multiple options (python, R, javascript). the weby stuff is done with plotly.js and uses d3.js underneath - https://github.com/plotly/plotly.js


What do you use instead?


I have tried so many plotting packages including Matplotlib but also others like R's base plotting, ggplot, Mathematica's plotting, etc. and I have to say I really dislike all of them. I am not sure why, but they all seem very counterintuitive compared to the way my mind things about plotting. This is especially true when it comes to formatting the plot. How I wish there were sane defaults for text size, etc.

As much as I like Python for it's fairly clean and elegant syntax, I actually prefer making charts in Excel/OpenOffice. I would love it if someone made a hybrid model where you could put plotting into code, but yet once the basic plot was created, a GUI could be used to modify the appearance of the plot without remembering the syntax for all the graphical parameters.

Maybe it could be a system where you run the code, and once the plot function is run in "interactive mode", an elegant GUI comes up and allows you to modify all the graphical parameters, and puts the required code into the plot function as parameters.


Have you tried graphpad prism? I think that's the closest plotting software to a hybrid. There's also spss.


Matplotlib combines the ease of use of MATLAB with the engineering precision of ... MATLAB. At least the programmers won't feel like tearing their hair out at the lunacy of MATLAB.

The only improvement could be to export the generated figures into excel so that we can make minor touchups manually.


Bah. Each time this comes up, people are quick to bash on Matlab. I won't stand for it. Matlab is not many things. It is certainly not all things to all men. But it is Fucking. Productive. for the kinds of quick numerical analyses that engineers often have to do. There is a function for everything and the kitchen sink, the syntax is simple as hell, there is a real IDE, and it gets out of your way.

I've tried all the rest. I've tried Julia. The tooling, my god, it is woeful. The famous Time To First Plot. I've tried Python. Doesn't hold a candle to Matlab for the ease with which you can manipulate matrices and vectors.

Matlab is far from perfect, but fuck it, it's an amazing tool for being productive.


Oh I agree, I only complain because I use it. By the way it's not Matlab but MATLAB. The name presumably comes from the Hindi exclamation for when you don't understand something.


Hahaha. I'm Indian, so that joke is familiar :) They should have just stuck a question mark at the end of it to represent all the leet programmers who couldn't fathom how a language could possibly dare to be 1-indexed.


> minor touchups manually

That's what I liked so much about matlab. You could generate figures programmatically and set the fonts and lines styles and whatnot, and then could just nudge stuff around in the figure if it wasn't how you wanted (plus retrieve that info).


The figure editor is the only real thing I miss from matlab. That and the ability to rotate the axes of plots interactively.

The rest of the language and the platform though? Awful.


I mostly used matlab for fourier transforming data and plotting it, with some occasional image processing thrown in and a few "simulations" in the sense of calculating some function at a bunch of points. For all of that, I thought it was great.

I saw people getting into make real applications, using the gui functionality and whatnot, it never felt like the right tool for something like that.


Outside of the core language of a glue for a bunch of numerical routines and stuff to plot them, it's a Frankenstein's monster.

You never know if the script you send someone will work right, because everyone has different toolbox options, and everything is in the global namespace. It has some OOP features bolted on to it, but it always looked worse than Perl5.

The core language is straight out of the mid-80s. The only real upgrade it got was it can finally broadcast matrix dimensions. Before that, you had people trying to 'vectorize' their code with 'repmat'.

Despite all of its shortcomings, especially with packaging, I'll take scientific python over that any day.


In the past, I've saved Matplotlib figures to .svg so I could touch them up in Inkscape. Not the most ergonomic process, but when push comes to shove it is workable.


Output an eps, then you can correct any issues manually in a text editor.


output in svg, touch up in inkscape


Matplotlib has been replaced by tikz [0] for most of the figures I need to generate (they would feed LaTeX docs anyway).

[0] https://tikz.dev/


I've adapted the same work flow as well. It's really nice to have one script that generates data (often taking a few minutes or hours) and then another (in TeX) that configures display. That way when I recycle plots from papers into slides for a talk, I can reconfigure these easily.


Tikz falls short when you have thousands of data points and/or need to do some scripting or filtering of the data. Scripting things is much nicer in Python than TeX.


Of all the python plotting packages I have tried, matplotlib is still my favorite. A large part of this is that's one of the few that doesn't insist on using a web browoser. My only complaint with it is I wish the plots were more interactive. What I really want is the speed and API of matplotlib with the interactivity of plotly but without the web browser. I think matlab may be the only thing that comes close to that.


For those who (like me) struggle to make consistent multigraph plots amongst other things I highly recommend proplot (https://proplot.readthedocs.io/en/stable/), a wrapper around matplotlib which takes away many of its pain points.


I suppose I'm the minority of people that enjoy using Matplotlib. I dont want a 'simplified' interface, I want to be able to do nearly anything and matplotlib provides that to me.


Matplotlib is awesome until you need to do advanced 3D plots where the lack of a proper 3D engine is becoming problematic. I had great hopes for Vispy but it seems like the original core developers moved back to their own plotting packages.


I won't touch Matplotlib with a 6 yard stick when there is ggplot.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: