Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 2a1b4ee

Browse files
committed
Also dynamically generate scalarmappable functions.
1 parent dedc5bd commit 2a1b4ee

File tree

1 file changed

+27
-40
lines changed

1 file changed

+27
-40
lines changed

lib/matplotlib/pyplot.py

Lines changed: 27 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -316,26 +316,8 @@ def rcdefaults():
316316
draw_all()
317317

318318

319-
# The current "image" (ScalarMappable) is retrieved or set
320-
# only via the pyplot interface using the following two
321-
# functions:
322-
def gci():
323-
"""
324-
Get the current colorable artist. Specifically, returns the
325-
current :class:`~matplotlib.cm.ScalarMappable` instance (image or
326-
patch collection), or *None* if no images or patch collections
327-
have been defined. The commands :func:`~matplotlib.pyplot.imshow`
328-
and :func:`~matplotlib.pyplot.figimage` create
329-
:class:`~matplotlib.image.Image` instances, and the commands
330-
:func:`~matplotlib.pyplot.pcolor` and
331-
:func:`~matplotlib.pyplot.scatter` create
332-
:class:`~matplotlib.collections.Collection` instances. The
333-
current image is an attribute of the current axes, or the nearest
334-
earlier axes in the current figure that contains an image.
335-
"""
336-
return gcf()._gci()
337-
338-
319+
# This function is not autogenerated because it is needed in when
320+
# autogenerating other functions, see `_post_processors`.
339321
def sci(im):
340322
"""
341323
Set the current image. This image will be the target of colormap
@@ -1991,23 +1973,6 @@ def getname_val(identifier):
19911973
fig.autofmt_xdate()
19921974

19931975

1994-
def _autogen_docstring(base):
1995-
"""Autogenerated wrappers will get their docstring from a base function
1996-
with an addendum."""
1997-
msg = ''
1998-
addendum = docstring.Appender(msg, '\n\n')
1999-
return lambda func: addendum(docstring.copy_dedent(base)(func))
2000-
2001-
2002-
# This function cannot be generated by boilerplate.py because it may
2003-
# return an image or a line.
2004-
@_autogen_docstring(Axes.spy)
2005-
def spy(Z, precision=0, marker=None, markersize=None, aspect='equal', **kwargs):
2006-
ret = ax.spy(Z, precision, marker, markersize, aspect, **kwargs)
2007-
if isinstance(ret, cm.ScalarMappable):
2008-
sci(ret)
2009-
return ret
2010-
20111976
# just to be safe. Interactive mode can be turned on without
20121977
# calling `plt.ion()` so register it again here.
20131978
# This is safe because multiple calls to `install_repl_displayhook`
@@ -2022,11 +1987,14 @@ def _make_wrapper(obj_getter, cls, command):
20221987
else:
20231988
method_name = func_name = command
20241989
method = getattr(cls, method_name)
1990+
post_processor = _post_processors.get(func_name, lambda obj: None)
20251991

20261992
def func(*args, **kwargs):
20271993
# We actually need to refetch the method here in case it has been
20281994
# monkey-patched.
2029-
return getattr(obj_getter(), method_name)(*args, **kwargs)
1995+
ret = getattr(obj_getter(), method_name)(*args, **kwargs)
1996+
post_processor(ret)
1997+
return ret
20301998

20311999
if six.PY2:
20322000
func.__name__ = func_name.encode("ascii")
@@ -2047,7 +2015,7 @@ def func(*args, **kwargs):
20472015
"clf", "figimage", "gca", "ginput", "savefig", "subplots_adjust",
20482016
"suptitle", "tight_layout", "waitforbuttonpress",
20492017
# Renamed commands.
2050-
"legend:figlegend", "text:figtext"]
2018+
"_gci:gci", "legend:figlegend", "text:figtext"]
20512019
_axes_commands = [
20522020
"acorr", "angle_spectrum", "annotate", "arrow", "autoscale", "axhline",
20532021
"axhspan", "axis", "axvline", "axvspan", "bar", "barbs", "barh", "boxplot",
@@ -2063,7 +2031,26 @@ def func(*args, **kwargs):
20632031
# Renamed commands.
20642032
"set_title:title", "set_xlabel:xlabel", "set_xscale:xscale",
20652033
"set_ylabel:ylabel", "set_yscale:yscale"]
2066-
2034+
_post_processors = {
2035+
# For the following functions, an additional callable will be called with
2036+
# the return value as argument.
2037+
"hexbin": sci,
2038+
"imshow": sci,
2039+
"pcolor": sci,
2040+
"pcolormesh": sci,
2041+
"quiver": sci,
2042+
"scatter": sci,
2043+
"tripcolor": sci,
2044+
"contour": lambda ret: sci(ret) if ret._A is not None else None,
2045+
"contourf": lambda ret: sci(ret) if ret._A is not None else None,
2046+
"tricontour": lambda ret: sci(ret) if ret._A is not None else None,
2047+
"tricontourf": lambda ret: sci(ret) if ret._A is not None else None,
2048+
"hist2d": lambda ret: sci(ret[-1]),
2049+
"specgram": lambda ret: sci(ret[-1]),
2050+
"streamplot": lambda ret: sci(ret.lines),
2051+
"spy": (
2052+
lambda ret: sci(ret) if isinstance(ret, cm.ScalarMappable) else None)
2053+
}
20672054

20682055
for _command in _canvas_commands:
20692056
_make_wrapper(

0 commit comments

Comments
 (0)