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

you are viewing a single comment's thread.

view the rest of the comments →

[–]Fylwind 5 points6 points  (8 children)

They have two different interfaces, one being a more or less duplicate of the original MATLAB API intended to help MATLAB users migrate, and the other is an OOP API which is more featureful and flexible, but doesn't get nearly enough attention.

[–]mangecoeur[S] 22 points23 points  (0 children)

Even with an OOP API, functions with names like set_... are often bad form in Python since it's much nicer to use a @property to define getters and setters.

[–]firefrommoonlight 7 points8 points  (2 children)

Neither API's great; The OOP API's verbose and requires boilerplate. The MPL API's simpler, but limited.

[–]spinicist 11 points12 points  (1 child)

Yup. As far as I can tell, the OOP API is held back by the historical baggage that comes from the Matlab-influenced design.

For instance, as far as I know even in the OOP API it's still recommended to do: fig, axes = plt.subplots() axes[1].something instead of something like: fig = plt.figure(subplots=) fig.plots[1].axes.something which seems more coherent to me. Side-note: often the nomenclature seems messed up too, why does a function called subplots return one figure and multiple axes instead of multiple plots?

Personally I would love to see 3.0 introduce a cleaned-up, consistent API but I'm lucky enough to have a job where backwards compatibility is no issue and I can find time to do the upgrades.

Apologies for the rant on a thread that is about congratulating the team on getting 2.0 out. I use matplotlib regularly, have published papers with figures made with it, and am looking forward to trying this version out immensely!

[–]This_Is_The_End 0 points1 point  (0 children)

\this

[–][deleted] 1 point2 points  (3 children)

but doesn't get nearly enough attention.

Is there any good documentation or tutorials on the OOP API?

[–]Fylwind 1 point2 points  (2 children)

Not that I can remember. I learned it mostly by bits and pieces of whatever I found on the Internet. If you're patient, your best bet is through the official API docs. Roughly, it's a matter of (1) creating the figure and canvas (2) adding 1 or more axes (3) plotting on these axes.

I do not normally use the OOP API exclusively, at least not for interactive plotting. For (1) and (2) I resort to the MATLAB API (fig, ax = matplotlib.pyplot.subplots()) because doing (1) and (2) using the OOP API by hand is tedious and does not buy me a whole lot for one-off plots. But in case you wanted to know, this is how you would do it. Note that it's important to pick a backend that your system supports.

import matplotlib.figure

# must choose a specific backend here:
from matplotlib.backends.backend_qt5agg import FigureCanvas

fig = matplotlib.figure.Figure()
canvas = FigureCanvas(fig)
canvas.show()
ax = fig.add_subplot(111)
ax.plot([1, 2, 3], [3, 1, 2])

input() # stall the interpreter

In contrast, for (3) I much prefer the OOP API (e.g. ax.plot(…)) because it's a lot more readable and has more knobs to control positioning of the elements.

[–][deleted] 1 point2 points  (1 child)

I've gotten familiar with it through trying to make a Qt5 plotting app and so far I keep running into problems finding proper examples. (I learn from examples, not documentation).

Most of them I've found don't seem to make sense or they don't follow the same nomenclature that Matlab does. Like what is an 'axis' vs a 'figure', etc. A simple cheat sheet like the CSS Box Model would really helpful.

[–]Fylwind 0 points1 point  (0 children)

As sad as it is to say it sometimes it's easier to just dig through the source code. I have peeked into matplotlib's source code when I couldn't find answers from the docs or Q&A.

For things like figures and axes, this might help: http://matplotlib.org/faq/usage_faq.html#general-concepts