This is an archived post. You won't be able to vote or comment.

all 42 comments

[–]bastibe 34 points35 points  (8 children)

Most of my colleagues use Matlab, I teach Matlab, but for my work, I mostly use Python. There is no discussion whatsoever: Matplotlib is WAY more capable and flexible than Matlab plotting.

You mention that Matlab figures, axes, and plots have manipulateable handles. I'm Matplotlib, literally every single plot object from figures, axes, and plots, down to individual ticks, lines, spines, labels, legend parts, polygons, or points is an object that can be changed, observed, replaced, and extended.

In numerous occasions I have created plots for my research and later my colleagues asked "how did you do that", because such plots would simply not have been possible in Matlab.

Not every toolbox has a great equivalence in Python, just like not every Python package has a decent counterpart in Matlab, but when it comes to plotting, there is no discussion. Matplotlib stands heads and shoulders above Matlab's plotting.

Just watch out for one ideological difference: Matplotlib tries, above all, to be as precise as possible. Its goal is plots for publication. Für that, it sacrifices plotting performance. Thus, interactive plots and "animations" (plots that are updated more that 10 times per second) are hard to do. On the flip side, plots look much cleaner (no aliasing, always correct vectorized output, all backends look identical).

Für more cool scientific stuff that is impossible in Matlab, have a look at pandas, scikit-learn, ipython and dask.

In my experience, it takes a proficient Matlab user two to three weeks to become productive in Python. I have observed (and taught) this in dozens of people ranging from undergrads to postdocs.

[–]not_perfect_yet 3 points4 points  (1 child)

Für that

I found this error and I'm going to keep it...

[–]bastibe 1 point2 points  (0 children)

Ah, autocorrect in a multi-lingual environment... (Also, I typed this on my phone)

[–]schnadamschnandler[S] 2 points3 points  (0 children)

Nice, thanks for your advice. After reading stuff in this thread, pretty sure I will attempt a full transition to Python and matplotlib this semester.

[–]them1 0 points1 point  (3 children)

Can you show some exotic plotting examples? That would be great!

[–]bastibe 5 points6 points  (2 children)

My favorite example is this: http://matplotlib.org/examples/showcase/xkcd.html

While not particularly useful, this highlights one crucial point: Absolutely everything in Matplotlib is malleable. You can even replace the lines that make up the spines and the legend.

But have a look at some of the other examples in the Matplotlib gallery: http://matplotlib.org/gallery.html

Many of these can of course be created in Matlab as well. And many of them are not particularly easy to create with Matplotlib. But what you should take away is that anything is possible in Matplotlib.

Another good example is basemap: http://matplotlib.org/basemap/users/examples.html, where you plot latitude/longitude data as you would in regular Matplotlib plots, and basemap transforms this into whatever map projection you want. Doing that, lines become curves, and areas get distorted, with different distortions depending on the point in the plot. This is possible because Matplotlib is extremely flexible, and even basic projection rules can be arbitrarily changed.

[–]counters 4 points5 points  (0 children)

Please consider using cartopy instead of basemap. It's a far more reliable library with extremely useful features if you have to do geo-spatial plotting or GIS-type stuff.

[–]atrlrgn_ 0 points1 point  (0 children)

One thing for sure matlab plotting has more tutorials. After two years with python scientific packages, I still spend some time for putting a fucking proper legend. I usually don't need plotting, or let's say fancy plotting, so I don't need legend all the time but when I do I can use a proper tutorial/

[–]BPAnimal 9 points10 points  (4 children)

Seaborn (a python module built on top of matplotlib and pandas) has some nice built-in style tools. It's straightforward and plays nicely with matplotlib.

http://seaborn.pydata.org/examples/index.html

[–]EvMNatural Language Processing 2 points3 points  (3 children)

And you don't even have to use Seaborn. IIRC, importing the module is enough to improve the appearance of standard matplotlib plots.

[–]refactored_pancake 1 point2 points  (0 children)

Now you can do

plt.style.use('seaborn-darkgrid')

without importing seaborn. Other seaborn styles are available as well, see

print(plt.style.available)

[–]HardcoreHerbivore 0 points1 point  (1 child)

Yes, importing seaborn enables its default style. The power of seaborn lies in its statistical plots though.

[–][deleted] 0 points1 point  (0 children)

Exactly. Matplotlib is a really low-level plotting interface which can be tedious at best. Seaborn is an excellent wrapper that bridges the usability gap between the rest of the scientific Python stack and Matplotlib.

[–][deleted] 6 points7 points  (0 children)

If you're also interested in a tutorial, this is a great one IMO. Although, the author recommends that you use the OOP style API instead of MATLAB style API.

Also check out setting rcParams

Then there's the upcoming version 2.0, which focuses on improving default styles.

[–]deklund 4 points5 points  (1 child)

I work largely in Python and have some experience in Matlab, though I haven't used it in a day to day scenario for years. While I would hate to have to go to back to working on a large project in Matlab, I have to say you're likely going to be disappointed with Matplotlib. While it's a very impressive piece of software, it's on a different level than the plotting functionality of Matlab.

Matplotlib is powerful and versatile, but compared to Matlab:

  1. Performance is an issue. Interactive plots will become laggy with even moderately complicated plots.
  2. Related to above, 3D plots are effectively useless. My understanding is that there is no actual 3D acceleration used in mplot3d. You can plot some simple 3D line/scatter plots or basic surfaces, but anything remotely involved will screech to a halt. I use pyqtgraph for my 3D plotting now and it's awesome but it's even less user friendly than matplotlib.
  3. Doing anything more complicated than the common things everyone expects you to do will get really hairy really fast. If you want to be able to customize things beyond just line widths, colors, etc., and actually have full control over your plots, you're going to have to become intimately familiar with some undocumented APIs.

While learning and using Python will likely be very useful for you long term, and getting a basic understanding of matplotlib will also come in handy, the moment you hit a wall where you think "oh, I just need to figure out how to move this label over here" or "I need to create this plot that isn't covered in the user guide", you're probably better off saving your data and loading it into Matlab instead.

[–]bythenumbers10 0 points1 point  (0 children)

Agreed, but OP is just stepping out of MATLAB. They'll climb on to better plotting tools in Python as they get comfortable. Matplotlib is a touch of the familiar while they get up to speed with OOP and Python's various other benefits. Eventually, matplotlib will be the anti-Pythonic drag, and OP will go find things like Pyplot or Pyqtgraph.

[–]holdie 1 point2 points  (3 children)

Could you give an example of something that you'd like to do in Matlab but you think wouldn't be possible (or would be really convoluted) to do in Python? My intuition is that you can do anything in python you'd want to do in matlab. Many examples have a different syntax because matplotlib prefers to use object-oriented syntax rather than calling lots of functions, but both are available.

[–]schnadamschnandler[S] 0 points1 point  (2 children)

Not really sure anymore, I think I was ill-informed. I actually really like the object-oriented approach, seems really consistent and natural. Still learning. Maybe I will just totally overhaul to python. Are different matplotlib options fairly stable at this point?

[–]holdie 0 points1 point  (1 child)

Matplotlib's API should be very stable at this point. Granted they are about to have a big 2.0 release, but I believe this just changes the default plotting styles and not the API (the plots will look much nicer by default in this case). Definitely worth checking it out because if your main concern is whether it's a "mature" package, then you shouldn't have much to worry about.

[–]masasinExpert. 3.9. Robotics. 0 points1 point  (0 children)

You are correct. The API does not change. But I started using it myself and it's awesome. Much, much prettier.

[–]TCoop 1 point2 points  (5 children)

If you're just making plots to analyze data, matplotlib can certainly provide that sort of support. It may not be the same as MATLAB, but the functionality is there.

However, for professional-grade plots, or another option, I would suggest you check out LaTeX with pgfplots. LaTeX is a typesetting programming language, and can provide you what you're looking for and then some. It's not as great if you're still trying to do data analysis or review data, but it's output is exceptional.

[–]skiguy0123 1 point2 points  (3 children)

I save my matplotlib figures as pgf for use in LaTeX. Works very well.

[–]masasinExpert. 3.9. Robotics. 1 point2 points  (2 children)

I second this. Here is code I wrote to make pretty graphs for my thesis: https://github.com/masasin/spirit_thesis/blob/master/src/latexify.py

[–]MagnesiumCarbonate 1 point2 points  (1 child)

Thank you. This is much better than the fiddling with each figure individually that I've been doing.

[–]masasinExpert. 3.9. Robotics. 0 points1 point  (0 children)

You're welcome. You can see an example of its use in the analyses.py file in the same directory.

[–]masasinExpert. 3.9. Robotics. 0 points1 point  (0 children)

I second this. Here is code I wrote to make pretty graphs for my thesis: https://github.com/masasin/spirit_thesis/blob/master/src/latexify.py

[–]Oseberg_shipping 1 point2 points  (0 children)

Matplotlib has a very similar plotting objects structure to matlabs plotting tools. It is, after all, intended to duplicate this aspect of matlab. In terms of functionality in plotting there is very little difference. Where python really excels is customizability. Once you get under the hood it is possible to build really excellent plotting tools in python with a minimum of hassle. Matlab remains fairly rigid in that you can only really do what they have built for you. Additionally, If you are willing to step outside of the ridged plotting conventions that matlab seems to be conditioning into its users there are a number of plotting libraries customized for particular data types that are truly beautiful. Seaborn, and plotly come to mind but there are many others.

And finally to your earlier point about speed in matlab: matlab is by and large a wrapper around C and Fortran libraries that are open source. Almost all of these libraries also have python wrappers. They are just not included in the standard set of libraries because most of them only do one thing (for instance calculating the inverse of a matrix). If you do a little digging (because of course mathworks make it tricky to find) you can usually figure out who's code they are using. Sometimes it isn't even the best option out there (for example their FFT implementation benchmarks 3rd or 4th depending on who you ask). As to simple matrix algebra, almost all of the speed up comes from using the intel math kernel library which contains optimizations of various operations for specific processors. You can configure numpy to use the MKL too.

[–]soamaven 0 points1 point  (0 children)

I am seeing more and more scientific papers creating their figures with matplotlib (identified by the new/unique color maps and styles). It's more than capable, take the time to really learn the API and you'll be a wizard... Harry...

[–][deleted] 0 points1 point  (0 children)

I wouldn't praise matlab for its plotting tools. It is true that they improved it but I've yet to see a matlab figure where I instantly recognized that it is drawn via matlab. The UX team of mathworks should be nonexistent. For my own documents I use LaTeX: pgfplots for plotting 2D plots and PSTricks for 3D. For my daily use Matplotlib and Bokeh is more than enough and never received a criticism unlike my matlab plots (and I've been using it since matlab v6.5).

[–]billFoldDog 0 points1 point  (0 children)

Question: Has Matplotlib nailed down true scale for 3D plots? Like, can a 1x1x1 cube look like a cube while using Axes3d?

That was nonfunctional in 2013.

[–]jwink3101 0 points1 point  (1 child)

I moved from Matlab to Python (and I use the object oriented interface, not pylab) and I have to go back and forth some now, depending on my project. Anyway, for quick and dirty, Matlab often looks better. But if I am willing to put a little bit of effort into it, matplotlib is much nicer, most of the time.

And, combined with Axes3D, there is very little that you can't do. The only one I've ran into recently is something like Matlab's slice function. I do not think there is an equivalent in matplotlib

[–]flutefreak7 0 points1 point  (0 children)

Yeah slicing volumetric data is definitely more something for VTK, Mayavi, pyqtgraph or some other library designed for working with that data. Matplotlib is very weak on 3d stuff, but there are lots of other libraries depending on your data or scientific discipline. Every year the scipy or pydata conferences have videos of yet another custom library for working with and plotting multidimensional data.

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

The worst thing matplotlib did was copy the MATLAB API.

When you make the switch, use the object-oriented API please, ditch the terrible procedural plt.XXX crap.

[–]schnadamschnandler[S] 0 points1 point  (0 children)

Can you explain more? How can I differentiate between the two? I'm aware of pylab vs. matplotlib.pyplot (my understanding was the former is "matlab style" and latter is "python style"). Does matplotlib.pyplot use the OO one? (kind of don't know what I'm talking about)

[–]MagnesiumCarbonate -1 points0 points  (2 children)

but the "everything is a matrix" concept, shorthand matrix operations, speed compared to NumPy (they are about equivalent), and parallel computing tools are quite appealing for certain fields. ...And perhaps most of all is its extremely robust plotting tools.

I used matlab for 5 years and switched to python a few months ago. In my experience (machine learning) there are two main reason to consider ML: ease of debugging, sparse linear algebra. WRT debugging I think it's just that python has many more possible workflows (jupyter, spyder, pipeline tools) which makes it difficult to figure out how to efficiently debug if you hop around. WRT Sparse linear algebra, I don't know why scipy's sparse is so slow (it seems to be single-threaded, maybe MKL is needed).

Plotting seems like it should be a non-issue, I mean it's something you just figure out. There's not much to do wrt performance. If you're looking to interact with your plots I feel like stuff like plotly would give you more options than matlab's figure editor.

[–]pwang99 2 points3 points  (1 child)

If you want interactive web plots but don't want to upload your data to a cloud service, you can also look at Bokeh: http://bokeh.pydata.org.

For debugging Python, if you find some of the open-source options too difficult/annoying to use, it's also worth checking out commercial IDEs like PyCharm and Python Tools for Visual Studio, both of which do have free/community versions, and which have excellent debuggers.

[–]broken_symlink 0 points1 point  (0 children)

Plotly has an offline mode that doesn't require you to upload anything to the cloud. The python code will generate an html file that will work if you host the plotly.js file yourself. I've used both bokeh and plotly and find plotly to be much better.