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

Skip to content

Commit 8b4544e

Browse files
committed
Also dynamically generate scalarmappable functions.
1 parent 01f8c87 commit 8b4544e

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
@@ -312,26 +312,8 @@ def rcdefaults():
312312
draw_all()
313313

314314

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

19811963

1982-
def _autogen_docstring(base):
1983-
"""Autogenerated wrappers will get their docstring from a base function
1984-
with an addendum."""
1985-
msg = ''
1986-
addendum = docstring.Appender(msg, '\n\n')
1987-
return lambda func: addendum(docstring.copy_dedent(base)(func))
1988-
1989-
1990-
# This function cannot be generated by boilerplate.py because it may
1991-
# return an image or a line.
1992-
@_autogen_docstring(Axes.spy)
1993-
def spy(Z, precision=0, marker=None, markersize=None, aspect='equal', **kwargs):
1994-
ret = ax.spy(Z, precision, marker, markersize, aspect, **kwargs)
1995-
if isinstance(ret, cm.ScalarMappable):
1996-
sci(ret)
1997-
return ret
1998-
19991964
# just to be safe. Interactive mode can be turned on without
20001965
# calling `plt.ion()` so register it again here.
20011966
# This is safe because multiple calls to `install_repl_displayhook`
@@ -2010,11 +1975,14 @@ def _make_wrapper(obj_getter, cls, command):
20101975
else:
20111976
method_name = func_name = command
20121977
method = getattr(cls, method_name)
1978+
post_processor = _post_processors.get(func_name, lambda obj: None)
20131979

20141980
def func(*args, **kwargs):
20151981
# We actually need to refetch the method here in case it has been
20161982
# monkey-patched.
2017-
return getattr(obj_getter(), method_name)(*args, **kwargs)
1983+
ret = getattr(obj_getter(), method_name)(*args, **kwargs)
1984+
post_processor(ret)
1985+
return ret
20181986

20191987
if six.PY2:
20201988
func.__name__ = func_name.encode("ascii")
@@ -2035,7 +2003,7 @@ def func(*args, **kwargs):
20352003
"clf", "figimage", "gca", "ginput", "savefig", "subplots_adjust",
20362004
"suptitle", "tight_layout", "waitforbuttonpress",
20372005
# Renamed commands.
2038-
"legend:figlegend", "text:figtext"]
2006+
"_gci:gci", "legend:figlegend", "text:figtext"]
20392007
_axes_commands = [
20402008
"acorr", "angle_spectrum", "annotate", "arrow", "autoscale", "axhline",
20412009
"axhspan", "axis", "axvline", "axvspan", "bar", "barbs", "barh", "boxplot",
@@ -2051,7 +2019,26 @@ def func(*args, **kwargs):
20512019
# Renamed commands.
20522020
"set_title:title", "set_xlabel:xlabel", "set_xscale:xscale",
20532021
"set_ylabel:ylabel", "set_yscale:yscale"]
2054-
2022+
_post_processors = {
2023+
# For the following functions, an additional callable will be called with
2024+
# the return value as argument.
2025+
"hexbin": sci,
2026+
"imshow": sci,
2027+
"pcolor": sci,
2028+
"pcolormesh": sci,
2029+
"quiver": sci,
2030+
"scatter": sci,
2031+
"tripcolor": sci,
2032+
"contour": lambda ret: sci(ret) if ret._A is not None else None,
2033+
"contourf": lambda ret: sci(ret) if ret._A is not None else None,
2034+
"tricontour": lambda ret: sci(ret) if ret._A is not None else None,
2035+
"tricontourf": lambda ret: sci(ret) if ret._A is not None else None,
2036+
"hist2d": lambda ret: sci(ret[-1]),
2037+
"specgram": lambda ret: sci(ret[-1]),
2038+
"streamplot": lambda ret: sci(ret.lines),
2039+
"spy": (
2040+
lambda ret: sci(ret) if isinstance(ret, cm.ScalarMappable) else None)
2041+
}
20552042

20562043
for _command in _canvas_commands:
20572044
_make_wrapper(

0 commit comments

Comments
 (0)