3 ms·
Definitely. One of the big problems, for me, is that there are at least two completely different interfaces with incompatible function names and arguments. Whil
by jbay808 5y ago
Definitely. One of the big problems, for me, is that there are at least two completely different interfaces with incompatible function names and arguments. While the object-oriented interface is frequently recommended, the pyplot interface is more commonly seen in stackoverflow questions and answers. Official documentation often shows examples only using one or the other. So you might have a mostly-functioning Object-Oriented plotting script, then wonder something like "how can I create another set of y-axis tics going along the right, showing temperature in fahrenheit", and only find slightly-similar-but-incompatible explanations that use the Pyplot interface.
It makes, by example, a pretty strong argument for not allowing multiple ways of doing something.
- curiousgal 5y agoWhat's the object-oriented interface? I've only ever used pyplot l.
- wswope 5y agofig, ax = plt.subplots() then make calls on the Figure and Axes objects. https://matplotlib.org/stable/api/index.html#usage-patterns https://matplotlib.org/stable/api/index.html#usage-patterns
- amelius 5y agoBut note that, while it doesn't expose global state, it is still not thread-safe. The documentation only says that it "may work" and that there are specific things that will certainly crash when used from different threads. https://matplotlib.org/stable/faq/howto_faq.html https://matplotlib.org/stable/faq/howto_faq.html
- rich_sasha 5y agoI got my head around it eventually. Anything imperative that implicitly operates on the current figure/axes just calls the OO method on gcf / gca (get current figure / axes). I often use the imperative API as it’s less typing often, and I do a lot of throwaway plotting code. But some functionality only exists in the OO interface. Then it’s an easy trip to gca().my_oo_function() and you recovered the imperative version.
- jbay808 5y agoThat's a good point. It might be less clear how to find the OO function that corresponds to a given imperative command.
- icegreentea2 5y agoKind of a crappy answer, but just read the source. pyplot is defined in a single file, so it's pretty search to search (for example here: https://matplotlib.org/stable/_modules/matplotlib/pyplot.html https://matplotlib.org/stable/_modules/matplotlib/pyplot.htm...). Or just clicky-click on 'source' link on the pyplot doc page for a call (for example: https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.axhspan.html https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot....) will take you to: https://matplotlib.org/stable/_modules/matplotlib/pyplot.html#axhspan https://matplotlib.org/stable/_modules/matplotlib/pyplot.htm... And for most (not all) of the functions, it's an easy read to figure out the OO call.