Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork7.9k
ENH: first draft of ensure_ax decorator#4488
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Changes from1 commit
ffdbe6e
a7918be
d271189
3501059
8351ceb
5feeb53
fe8eb34
9c0859b
f9ace37
c44b7dd
e398ad8
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
Not sure if this is a good idea. It is different that thecurrent behavior and still a bit too magical/implicit.
- Loading branch information
Uh oh!
There was an error while loading.Please reload this page.
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -205,6 +205,22 @@ def uninstall_repl_displayhook(): | ||
""" | ||
_ENSURE_AX_NEW_DOC = """ | ||
This function has been decorated by pyplot to create a new | ||
axes if one is not explicitly passed. | ||
The wrapped function can be called as any of :: | ||
{obj}{func}(*args, **kwargs) | ||
{obj}{func}(ax, *args, **kwargs) | ||
{obj}{func}(.., ax=ax) | ||
The first will make a new figure and axes, the other two | ||
will add to the axes passed in. | ||
""" | ||
def ensure_ax(func): | ||
"""Decorator to ensure that the function gets an `Axes` object. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. As I read this line, having not read the source yet, I was expecting this decorator to create a figure/axes pair ( There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. If these catch on, that is probably worth it's own decorator. This mimics the expected behavior of plot=ensure_ax(Axes.axes.plot)hist=ensure_ax(Axes.axes.hist)... | ||
@@ -254,6 +270,34 @@ def inner(*args, **kwargs): | ||
return inner | ||
def ensure_new_ax(func): | ||
"""Decorator to ensure that the function gets a new `Axes` object. | ||
Same as ensure_ax expect that a new figure and axes are created | ||
if an Axes is not explicitly passed. | ||
""" | ||
@wraps(func) | ||
def inner(*args, **kwargs): | ||
if 'ax' in kwargs: | ||
ax = kwargs.pop('ax') | ||
elif len(args) > 0 and isinstance(args[0], Axes): | ||
ax = args[0] | ||
args = args[1:] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. why not just pop? One line instead of two... ax=args.pop(0) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. Because There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. Erm... good point, I always thought of args as a list, upon reading the docs I see it as as a tuple 😊. | ||
else: | ||
ax = quick_axes() | ||
return func(ax, *args, **kwargs) | ||
pre_doc = inner.__doc__ | ||
if pre_doc is None: | ||
pre_doc = '' | ||
else: | ||
pre_doc = dedent(pre_doc) | ||
inner.__doc__ = (_ENSURE_AX_NEW_DOC.format(func=func.__name__, obj='') + | ||
pre_doc) | ||
return inner | ||
def ensure_ax_meth(func): | ||
""" | ||
The same as ensure axes, but for class methods :: | ||