From a7291a86e90f15e212f15c7ccb279314c44c2d52 Mon Sep 17 00:00:00 2001 From: Nelle Varoquaux Date: Thu, 11 Jul 2013 13:25:13 +0200 Subject: [PATCH 01/12] MAINT moved axhline into it's own module This is part of the refactoring process. All plots methods should be moved to their own modules. In this commit, I've started the refactoring by moving the axhline to its own module, lines, which should contain lines and spans plotting function. --- lib/matplotlib/axes/_axes.py | 74 ++------------------------------ lib/matplotlib/axes/_lines.py | 80 +++++++++++++++++++++++++++++++++++ 2 files changed, 84 insertions(+), 70 deletions(-) create mode 100644 lib/matplotlib/axes/_lines.py diff --git a/lib/matplotlib/axes/_axes.py b/lib/matplotlib/axes/_axes.py index e72c0ba38bd7..166983bfd44f 100644 --- a/lib/matplotlib/axes/_axes.py +++ b/lib/matplotlib/axes/_axes.py @@ -34,6 +34,8 @@ from matplotlib.container import BarContainer, ErrorbarContainer, StemContainer from matplotlib.axes._base import _AxesBase +from . import _lines + iterable = cbook.iterable is_string_like = cbook.is_string_like is_sequence_of_strings = cbook.is_sequence_of_strings @@ -591,77 +593,9 @@ def annotate(self, *args, **kwargs): #### Lines and spans - @docstring.dedent_interpd def axhline(self, y=0, xmin=0, xmax=1, **kwargs): - """ - Add a horizontal line across the axis. - - Parameters - ---------- - y : scalar, optional, default: 0 - y position in data coordinates of the horizontal line. - - xmin : scalar, optional, default: 0 - Should be between 0 and 1, 0 being the far left of the plot, 1 the - far right of the plot. - - xmax : scalar, optional, default: 1 - Should be between 0 and 1, 0 being the far left of the plot, 1 the - far right of the plot. - - Returns - ------- - `~matplotlib.lines.Line2D` - - Notes - ----- - kwargs are the same as kwargs to plot, and can be - used to control the line properties. e.g., - - Examples - -------- - - * draw a thick red hline at 'y' = 0 that spans the xrange:: - - >>> axhline(linewidth=4, color='r') - - * draw a default hline at 'y' = 1 that spans the xrange:: - - >>> axhline(y=1) - - * draw a default hline at 'y' = .5 that spans the the middle half of - the xrange:: - - >>> axhline(y=.5, xmin=0.25, xmax=0.75) - - Valid kwargs are :class:`~matplotlib.lines.Line2D` properties, - with the exception of 'transform': - - %(Line2D)s - - See also - -------- - `axhspan` for example plot and source code - """ - - if "transform" in kwargs: - raise ValueError( - "'transform' is not allowed as a kwarg;" - + "axhline generates its own transform.") - ymin, ymax = self.get_ybound() - - # We need to strip away the units for comparison with - # non-unitized bounds - self._process_unit_info(ydata=y, kwargs=kwargs) - yy = self.convert_yunits(y) - scaley = (yy < ymin) or (yy > ymax) - - trans = mtransforms.blended_transform_factory( - self.transAxes, self.transData) - l = mlines.Line2D([xmin, xmax], [y, y], transform=trans, **kwargs) - self.add_line(l) - self.autoscale_view(scalex=False, scaley=scaley) - return l + return _lines.axhline(self, y=0, xmin=0, xmax=1, **kwargs) + axhline.__doc__ = _lines.axhline.__doc__ @docstring.dedent_interpd def axvline(self, x=0, ymin=0, ymax=1, **kwargs): diff --git a/lib/matplotlib/axes/_lines.py b/lib/matplotlib/axes/_lines.py new file mode 100644 index 000000000000..709f4b9bdb5b --- /dev/null +++ b/lib/matplotlib/axes/_lines.py @@ -0,0 +1,80 @@ +""" +Lines and spans +""" + +from matplotlib import docstring +from matplotlib import transforms as mtransforms +from matplotlib import line as mlines + + +@docstring.dedent_interpd +def axhline(ax, y=0, xmin=0, xmax=1, **kwargs): + """ + Add a horizontal line across the axis. + + Parameters + ---------- + y : scalar, optional, default: 0 + y position in data coordinates of the horizontal line. + + xmin : scalar, optional, default: 0 + Should be between 0 and 1, 0 being the far left of the plot, 1 the + far right of the plot. + + xmax : scalar, optional, default: 1 + Should be between 0 and 1, 0 being the far left of the plot, 1 the + far right of the plot. + + Returns + ------- + `~matplotlib.lines.Line2D` + + Notes + ----- + kwargs are the same as kwargs to plot, and can be + used to control the line properties. e.g., + + Examples + -------- + + * draw a thick red hline at 'y' = 0 that spans the xrange:: + + >>> axhline(linewidth=4, color='r') + + * draw a default hline at 'y' = 1 that spans the xrange:: + + >>> axhline(y=1) + + * draw a default hline at 'y' = .5 that spans the the middle half of + the xrange:: + + >>> axhline(y=.5, xmin=0.25, xmax=0.75) + + Valid kwargs are :class:`~matplotlib.lines.Line2D` properties, + with the exception of 'transform': + + %(Line2D)s + + See also + -------- + `axhspan` for example plot and source code + """ + + if "transform" in kwargs: + raise ValueError( + "'transform' is not allowed as a kwarg;" + + "axhline generates its own transform.") + ymin, ymax = ax.get_ybound() + + # We need to strip away the units for comparison with + # non-unitized bounds + ax._process_unit_info(ydata=y, kwargs=kwargs) + yy = ax.convert_yunits(y) + scaley = (yy < ymin) or (yy > ymax) + + trans = mtransforms.blended_transform_factory( + ax.transAxes, ax.transData) + l = mlines.Line2D([xmin, xmax], [y, y], transform=trans, **kwargs) + ax.add_line(l) + ax.autoscale_view(scalex=False, scaley=scaley) + return l From cb5237665814c8c9a1b89672ce8bb9d186f1feed Mon Sep 17 00:00:00 2001 From: Nelle Varoquaux Date: Thu, 11 Jul 2013 13:46:30 +0200 Subject: [PATCH 02/12] FIX typo in an import --- lib/matplotlib/axes/_lines.py | 89 ++++++++++++++++++++++++++++++++++- 1 file changed, 88 insertions(+), 1 deletion(-) diff --git a/lib/matplotlib/axes/_lines.py b/lib/matplotlib/axes/_lines.py index 709f4b9bdb5b..f3a11751d710 100644 --- a/lib/matplotlib/axes/_lines.py +++ b/lib/matplotlib/axes/_lines.py @@ -4,7 +4,7 @@ from matplotlib import docstring from matplotlib import transforms as mtransforms -from matplotlib import line as mlines +from matplotlib import lines as mlines @docstring.dedent_interpd @@ -78,3 +78,90 @@ def axhline(ax, y=0, xmin=0, xmax=1, **kwargs): ax.add_line(l) ax.autoscale_view(scalex=False, scaley=scaley) return l + + +def hlines(ax, y, xmin, xmax, colors='k', linestyles='solid', + label='', **kwargs): + """ + Plot horizontal lines at each `y` from `xmin` to `xmax`. + + Parameters + ---------- + y : scalar or sequence of scalar + y-indexes where to plot the lines. + + xmin, xmax : scalar or 1D array_like + Respective beginning and end of each line. If scalars are + provided, all lines will have same length. + + colors : array_like of colors, optional, default: 'k' + + linestyles : ['solid' | 'dashed' | 'dashdot' | 'dotted'], optional + + label : string, optional, default: '' + + Returns + ------- + lines : `~matplotlib.collections.LineCollection` + + Other parameters + ---------------- + kwargs : `~matplotlib.collections.LineCollection` properties. + + See also + -------- + vlines : vertical lines + + Examples + -------- + .. plot:: mpl_examples/pylab_examples/vline_hline_demo.py + + """ + + # We do the conversion first since not all unitized data is uniform + # process the unit information + ax._process_unit_info([xmin, xmax], y, kwargs=kwargs) + y = ax.convert_yunits(y) + xmin = ax.convert_xunits(xmin) + xmax = ax.convert_xunits(xmax) + + if not iterable(y): + y = [y] + if not iterable(xmin): + xmin = [xmin] + if not iterable(xmax): + xmax = [xmax] + + y = np.asarray(y) + xmin = np.asarray(xmin) + xmax = np.asarray(xmax) + + if len(xmin) == 1: + xmin = np.resize(xmin, y.shape) + if len(xmax) == 1: + xmax = np.resize(xmax, y.shape) + + if len(xmin) != len(y): + raise ValueError('xmin and y are unequal sized sequences') + if len(xmax) != len(y): + raise ValueError('xmax and y are unequal sized sequences') + + verts = [((thisxmin, thisy), (thisxmax, thisy)) + for thisxmin, thisxmax, thisy in zip(xmin, xmax, y)] + coll = mcoll.LineCollection(verts, colors=colors, + linestyles=linestyles, label=label) + ax.add_collection(coll) + coll.update(kwargs) + + if len(y) > 0: + minx = min(xmin.min(), xmax.min()) + maxx = max(xmin.max(), xmax.max()) + miny = y.min() + maxy = y.max() + + corners = (minx, miny), (maxx, maxy) + + ax.update_datalim(corners) + ax.autoscale_view() + + return coll From d3c8b78c1d64b0a66888a34d95c06b44d6f952c8 Mon Sep 17 00:00:00 2001 From: Nelle Varoquaux Date: Thu, 11 Jul 2013 13:49:39 +0200 Subject: [PATCH 03/12] FIX missing imports --- lib/matplotlib/axes/_lines.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/lib/matplotlib/axes/_lines.py b/lib/matplotlib/axes/_lines.py index f3a11751d710..3d0cbbbeaa7c 100644 --- a/lib/matplotlib/axes/_lines.py +++ b/lib/matplotlib/axes/_lines.py @@ -1,10 +1,13 @@ """ Lines and spans """ +import numpy as np from matplotlib import docstring from matplotlib import transforms as mtransforms from matplotlib import lines as mlines +from matplotlib import collections as mcoll +from matplotlib.cbook import iterable @docstring.dedent_interpd @@ -81,7 +84,7 @@ def axhline(ax, y=0, xmin=0, xmax=1, **kwargs): def hlines(ax, y, xmin, xmax, colors='k', linestyles='solid', - label='', **kwargs): + label='', **kwargs): """ Plot horizontal lines at each `y` from `xmin` to `xmax`. @@ -147,7 +150,7 @@ def hlines(ax, y, xmin, xmax, colors='k', linestyles='solid', raise ValueError('xmax and y are unequal sized sequences') verts = [((thisxmin, thisy), (thisxmax, thisy)) - for thisxmin, thisxmax, thisy in zip(xmin, xmax, y)] + for thisxmin, thisxmax, thisy in zip(xmin, xmax, y)] coll = mcoll.LineCollection(verts, colors=colors, linestyles=linestyles, label=label) ax.add_collection(coll) From 005aa23d46d071481ac44502e2e4971ea9fcdacb Mon Sep 17 00:00:00 2001 From: Nelle Varoquaux Date: Thu, 11 Jul 2013 13:55:15 +0200 Subject: [PATCH 04/12] MAINT moved hlines to the _lines module --- lib/matplotlib/axes/_axes.py | 89 ++---------------------------------- 1 file changed, 4 insertions(+), 85 deletions(-) diff --git a/lib/matplotlib/axes/_axes.py b/lib/matplotlib/axes/_axes.py index 166983bfd44f..a8e12d07ecf3 100644 --- a/lib/matplotlib/axes/_axes.py +++ b/lib/matplotlib/axes/_axes.py @@ -10,7 +10,6 @@ rcParams = matplotlib.rcParams import matplotlib.cbook as cbook -from matplotlib.cbook import _string_to_bool import matplotlib.collections as mcoll import matplotlib.colors as mcolors import matplotlib.contour as mcontour @@ -780,90 +779,10 @@ def axvspan(self, xmin, xmax, ymin=0, ymax=1, **kwargs): @docstring.dedent def hlines(self, y, xmin, xmax, colors='k', linestyles='solid', label='', **kwargs): - """ - Plot horizontal lines at each `y` from `xmin` to `xmax`. - - Parameters - ---------- - y : scalar or sequence of scalar - y-indexes where to plot the lines. - - xmin, xmax : scalar or 1D array_like - Respective beginning and end of each line. If scalars are - provided, all lines will have same length. - - colors : array_like of colors, optional, default: 'k' - - linestyles : ['solid' | 'dashed' | 'dashdot' | 'dotted'], optional - - label : string, optional, default: '' - - Returns - ------- - lines : `~matplotlib.collections.LineCollection` - - Other parameters - ---------------- - kwargs : `~matplotlib.collections.LineCollection` properties. - - See also - -------- - vlines : vertical lines - - Examples - -------- - .. plot:: mpl_examples/pylab_examples/vline_hline_demo.py - - """ - - # We do the conversion first since not all unitized data is uniform - # process the unit information - self._process_unit_info([xmin, xmax], y, kwargs=kwargs) - y = self.convert_yunits(y) - xmin = self.convert_xunits(xmin) - xmax = self.convert_xunits(xmax) - - if not iterable(y): - y = [y] - if not iterable(xmin): - xmin = [xmin] - if not iterable(xmax): - xmax = [xmax] - - y = np.asarray(y) - xmin = np.asarray(xmin) - xmax = np.asarray(xmax) - - if len(xmin) == 1: - xmin = np.resize(xmin, y.shape) - if len(xmax) == 1: - xmax = np.resize(xmax, y.shape) - - if len(xmin) != len(y): - raise ValueError('xmin and y are unequal sized sequences') - if len(xmax) != len(y): - raise ValueError('xmax and y are unequal sized sequences') - - verts = [((thisxmin, thisy), (thisxmax, thisy)) - for thisxmin, thisxmax, thisy in zip(xmin, xmax, y)] - coll = mcoll.LineCollection(verts, colors=colors, - linestyles=linestyles, label=label) - self.add_collection(coll) - coll.update(kwargs) - - if len(y) > 0: - minx = min(xmin.min(), xmax.min()) - maxx = max(xmin.max(), xmax.max()) - miny = y.min() - maxy = y.max() - - corners = (minx, miny), (maxx, maxy) - - self.update_datalim(corners) - self.autoscale_view() - - return coll - + return _lines.hlines(self, y, xmin, xmax, colors='k', + linestyles='solid', label='', **kwargs) + hlines.__doc__ = _lines.hlines.__doc__ + @docstring.dedent_interpd def vlines(self, x, ymin, ymax, colors='k', linestyles='solid', label='', **kwargs): From b96ae450d5091323d24ab8c7215dc769b406208c Mon Sep 17 00:00:00 2001 From: Nelle Varoquaux Date: Thu, 11 Jul 2013 14:15:06 +0200 Subject: [PATCH 05/12] MAINT moved more plotting functions to _lines. --- lib/matplotlib/axes/_axes.py | 271 +-------------------------------- lib/matplotlib/axes/_lines.py | 274 +++++++++++++++++++++++++++++++++- 2 files changed, 280 insertions(+), 265 deletions(-) diff --git a/lib/matplotlib/axes/_axes.py b/lib/matplotlib/axes/_axes.py index a8e12d07ecf3..5ce38965da28 100644 --- a/lib/matplotlib/axes/_axes.py +++ b/lib/matplotlib/axes/_axes.py @@ -596,283 +596,28 @@ def axhline(self, y=0, xmin=0, xmax=1, **kwargs): return _lines.axhline(self, y=0, xmin=0, xmax=1, **kwargs) axhline.__doc__ = _lines.axhline.__doc__ - @docstring.dedent_interpd def axvline(self, x=0, ymin=0, ymax=1, **kwargs): - """ - Add a vertical line across the axes. - - Parameters - ---------- - x : scalar, optional, default: 0 - y position in data coordinates of the vertical line. - - ymin : scalar, optional, default: 0 - Should be between 0 and 1, 0 being the far left of the plot, 1 the - far right of the plot. - - ymax : scalar, optional, default: 1 - Should be between 0 and 1, 0 being the far left of the plot, 1 the - far right of the plot. - - Returns - ------- - `~matplotlib.lines.Line2D` - - - Examples - --------- - * draw a thick red vline at *x* = 0 that spans the yrange:: - - >>> axvline(linewidth=4, color='r') - - * draw a default vline at *x* = 1 that spans the yrange:: + return _lines.axvline(self, x=0, ymin=0, ymax=1, **kwargs) + axvline.__doc__ = _lines.axvline.__doc__ - >>> axvline(x=1) - - * draw a default vline at *x* = .5 that spans the the middle half of - the yrange:: - - >>> axvline(x=.5, ymin=0.25, ymax=0.75) - - Valid kwargs are :class:`~matplotlib.lines.Line2D` properties, - with the exception of 'transform': - - %(Line2D)s - - See also - -------- - - `axhspan` for example plot and source code - """ - - if "transform" in kwargs: - raise ValueError( - "'transform' is not allowed as a kwarg;" - + "axvline generates its own transform.") - xmin, xmax = self.get_xbound() - - # We need to strip away the units for comparison with - # non-unitized bounds - self._process_unit_info(xdata=x, kwargs=kwargs) - xx = self.convert_xunits(x) - scalex = (xx < xmin) or (xx > xmax) - - trans = mtransforms.blended_transform_factory( - self.transData, self.transAxes) - l = mlines.Line2D([x, x], [ymin, ymax], transform=trans, **kwargs) - self.add_line(l) - self.autoscale_view(scalex=scalex, scaley=False) - return l - - @docstring.dedent_interpd def axhspan(self, ymin, ymax, xmin=0, xmax=1, **kwargs): - """ - Add a horizontal span (rectangle) across the axis. - - Call signature:: - - axhspan(ymin, ymax, xmin=0, xmax=1, **kwargs) - - *y* coords are in data units and *x* coords are in axes (relative - 0-1) units. - - Draw a horizontal span (rectangle) from *ymin* to *ymax*. - With the default values of *xmin* = 0 and *xmax* = 1, this - always spans the xrange, regardless of the xlim settings, even - if you change them, e.g., with the :meth:`set_xlim` command. - That is, the horizontal extent is in axes coords: 0=left, - 0.5=middle, 1.0=right but the *y* location is in data - coordinates. - - Return value is a :class:`matplotlib.patches.Polygon` - instance. - - Examples: - - * draw a gray rectangle from *y* = 0.25-0.75 that spans the - horizontal extent of the axes:: - - >>> axhspan(0.25, 0.75, facecolor='0.5', alpha=0.5) - - Valid kwargs are :class:`~matplotlib.patches.Polygon` properties: - - %(Polygon)s - - **Example:** - - .. plot:: mpl_examples/pylab_examples/axhspan_demo.py - - """ - trans = mtransforms.blended_transform_factory( - self.transAxes, self.transData) - - # process the unit information - self._process_unit_info([xmin, xmax], [ymin, ymax], kwargs=kwargs) - - # first we need to strip away the units - xmin, xmax = self.convert_xunits([xmin, xmax]) - ymin, ymax = self.convert_yunits([ymin, ymax]) - - verts = (xmin, ymin), (xmin, ymax), (xmax, ymax), (xmax, ymin) - p = mpatches.Polygon(verts, **kwargs) - p.set_transform(trans) - self.add_patch(p) - self.autoscale_view(scalex=False) - return p + return _lines.axhspan(self, ymin, ymax, xmin=0, xmax=1, **kwargs) + axhspan.__doc__ = _lines.axhspan.__doc__ - @docstring.dedent_interpd def axvspan(self, xmin, xmax, ymin=0, ymax=1, **kwargs): - """ - Add a vertical span (rectangle) across the axes. - - Call signature:: - - axvspan(xmin, xmax, ymin=0, ymax=1, **kwargs) - - *x* coords are in data units and *y* coords are in axes (relative - 0-1) units. - - Draw a vertical span (rectangle) from *xmin* to *xmax*. With - the default values of *ymin* = 0 and *ymax* = 1, this always - spans the yrange, regardless of the ylim settings, even if you - change them, e.g., with the :meth:`set_ylim` command. That is, - the vertical extent is in axes coords: 0=bottom, 0.5=middle, - 1.0=top but the *y* location is in data coordinates. - - Return value is the :class:`matplotlib.patches.Polygon` - instance. - - Examples: - - * draw a vertical green translucent rectangle from x=1.25 to 1.55 that - spans the yrange of the axes:: - - >>> axvspan(1.25, 1.55, facecolor='g', alpha=0.5) - - Valid kwargs are :class:`~matplotlib.patches.Polygon` - properties: - - %(Polygon)s - - .. seealso:: - - :meth:`axhspan` - for example plot and source code - """ - trans = mtransforms.blended_transform_factory( - self.transData, self.transAxes) - - # process the unit information - self._process_unit_info([xmin, xmax], [ymin, ymax], kwargs=kwargs) + return _lines.axvspan(self, xmin, xmax, ymin=0, ymax=1, **kwargs) + axvspan.__doc__ = _lines.axvspan.__doc__ - # first we need to strip away the units - xmin, xmax = self.convert_xunits([xmin, xmax]) - ymin, ymax = self.convert_yunits([ymin, ymax]) - - verts = [(xmin, ymin), (xmin, ymax), (xmax, ymax), (xmax, ymin)] - p = mpatches.Polygon(verts, **kwargs) - p.set_transform(trans) - self.add_patch(p) - self.autoscale_view(scaley=False) - return p - - @docstring.dedent def hlines(self, y, xmin, xmax, colors='k', linestyles='solid', label='', **kwargs): return _lines.hlines(self, y, xmin, xmax, colors='k', linestyles='solid', label='', **kwargs) hlines.__doc__ = _lines.hlines.__doc__ - @docstring.dedent_interpd def vlines(self, x, ymin, ymax, colors='k', linestyles='solid', label='', **kwargs): - """ - Plot vertical lines. - - Plot vertical lines at each `x` from `ymin` to `ymax`. - - Parameters - ---------- - x : scalar or 1D array_like - x-indexes where to plot the lines. - - xmin, xmax : scalar or 1D array_like - Respective beginning and end of each line. If scalars are - provided, all lines will have same length. - - colors : array_like of colors, optional, default: 'k' - - linestyles : ['solid' | 'dashed' | 'dashdot' | 'dotted'], optional - - label : string, optional, default: '' - - Returns - ------- - lines : `~matplotlib.collections.LineCollection` - - Other parameters - ---------------- - kwargs : `~matplotlib.collections.LineCollection` properties. - - See also - -------- - hlines : horizontal lines - - Examples - --------- - .. plot:: mpl_examples/pylab_examples/vline_hline_demo.py - - """ - - self._process_unit_info(xdata=x, ydata=[ymin, ymax], kwargs=kwargs) - - # We do the conversion first since not all unitized data is uniform - x = self.convert_xunits(x) - ymin = self.convert_yunits(ymin) - ymax = self.convert_yunits(ymax) - - if not iterable(x): - x = [x] - if not iterable(ymin): - ymin = [ymin] - if not iterable(ymax): - ymax = [ymax] - - x = np.asarray(x) - ymin = np.asarray(ymin) - ymax = np.asarray(ymax) - if len(ymin) == 1: - ymin = np.resize(ymin, x.shape) - if len(ymax) == 1: - ymax = np.resize(ymax, x.shape) - - if len(ymin) != len(x): - raise ValueError('ymin and x are unequal sized sequences') - if len(ymax) != len(x): - raise ValueError('ymax and x are unequal sized sequences') - - Y = np.array([ymin, ymax]).T - - verts = [((thisx, thisymin), (thisx, thisymax)) - for thisx, (thisymin, thisymax) in zip(x, Y)] - #print 'creating line collection' - coll = mcoll.LineCollection(verts, colors=colors, - linestyles=linestyles, label=label) - self.add_collection(coll) - coll.update(kwargs) - - if len(x) > 0: - minx = min(x) - maxx = max(x) - - miny = min(min(ymin), min(ymax)) - maxy = max(max(ymin), max(ymax)) - - corners = (minx, miny), (maxx, maxy) - self.update_datalim(corners) - self.autoscale_view() - - return coll + return _lines.vlines.__doc__ + vlines.__doc__ = _lines.vlines.__doc__ @docstring.dedent_interpd def eventplot(self, positions, orientation='horizontal', lineoffsets=1, diff --git a/lib/matplotlib/axes/_lines.py b/lib/matplotlib/axes/_lines.py index 3d0cbbbeaa7c..dde03638795d 100644 --- a/lib/matplotlib/axes/_lines.py +++ b/lib/matplotlib/axes/_lines.py @@ -3,14 +3,12 @@ """ import numpy as np -from matplotlib import docstring from matplotlib import transforms as mtransforms from matplotlib import lines as mlines from matplotlib import collections as mcoll from matplotlib.cbook import iterable -@docstring.dedent_interpd def axhline(ax, y=0, xmin=0, xmax=1, **kwargs): """ Add a horizontal line across the axis. @@ -83,6 +81,74 @@ def axhline(ax, y=0, xmin=0, xmax=1, **kwargs): return l +def axvline(ax, x=0, ymin=0, ymax=1, **kwargs): + """ + Add a vertical line across the axes. + + Parameters + ---------- + x : scalar, optional, default: 0 + y position in data coordinates of the vertical line. + + ymin : scalar, optional, default: 0 + Should be between 0 and 1, 0 being the far left of the plot, 1 the + far right of the plot. + + ymax : scalar, optional, default: 1 + Should be between 0 and 1, 0 being the far left of the plot, 1 the + far right of the plot. + + Returns + ------- + `~matplotlib.lines.Line2D` + + + Examples + --------- + * draw a thick red vline at *x* = 0 that spans the yrange:: + + >>> axvline(linewidth=4, color='r') + + * draw a default vline at *x* = 1 that spans the yrange:: + + >>> axvline(x=1) + + * draw a default vline at *x* = .5 that spans the the middle half of + the yrange:: + + >>> axvline(x=.5, ymin=0.25, ymax=0.75) + + Valid kwargs are :class:`~matplotlib.lines.Line2D` properties, + with the exception of 'transform': + + %(Line2D)s + + See also + -------- + + `axhspan` for example plot and source code + """ + + if "transform" in kwargs: + raise ValueError( + "'transform' is not allowed as a kwarg;" + + "axvline generates its own transform.") + xmin, xmax = ax.get_xbound() + + # We need to strip away the units for comparison with + # non-unitized bounds + ax._process_unit_info(xdata=x, kwargs=kwargs) + xx = ax.convert_xunits(x) + scalex = (xx < xmin) or (xx > xmax) + + trans = mtransforms.blended_transform_factory( + ax.transData, ax.transAxes) + l = mlines.Line2D([x, x], [ymin, ymax], transform=trans, **kwargs) + ax.add_line(l) + ax.autoscale_view(scalex=scalex, scaley=False) + return l + + def hlines(ax, y, xmin, xmax, colors='k', linestyles='solid', label='', **kwargs): """ @@ -168,3 +234,207 @@ def hlines(ax, y, xmin, xmax, colors='k', linestyles='solid', ax.autoscale_view() return coll + + +def vlines(ax, x, ymin, ymax, colors='k', linestyles='solid', + label='', **kwargs): + """ + Plot vertical lines. + + Plot vertical lines at each `x` from `ymin` to `ymax`. + + Parameters + ---------- + x : scalar or 1D array_like + x-indexes where to plot the lines. + + xmin, xmax : scalar or 1D array_like + Respective beginning and end of each line. If scalars are + provided, all lines will have same length. + + colors : array_like of colors, optional, default: 'k' + + linestyles : ['solid' | 'dashed' | 'dashdot' | 'dotted'], optional + + label : string, optional, default: '' + + Returns + ------- + lines : `~matplotlib.collections.LineCollection` + + Other parameters + ---------------- + kwargs : `~matplotlib.collections.LineCollection` properties. + + See also + -------- + hlines : horizontal lines + + Examples + --------- + .. plot:: mpl_examples/pylab_examples/vline_hline_demo.py + + """ + + ax._process_unit_info(xdata=x, ydata=[ymin, ymax], kwargs=kwargs) + + # We do the conversion first since not all unitized data is uniform + x = ax.convert_xunits(x) + ymin = ax.convert_yunits(ymin) + ymax = ax.convert_yunits(ymax) + + if not iterable(x): + x = [x] + if not iterable(ymin): + ymin = [ymin] + if not iterable(ymax): + ymax = [ymax] + + x = np.asarray(x) + ymin = np.asarray(ymin) + ymax = np.asarray(ymax) + if len(ymin) == 1: + ymin = np.resize(ymin, x.shape) + if len(ymax) == 1: + ymax = np.resize(ymax, x.shape) + + if len(ymin) != len(x): + raise ValueError('ymin and x are unequal sized sequences') + if len(ymax) != len(x): + raise ValueError('ymax and x are unequal sized sequences') + + Y = np.array([ymin, ymax]).T + + verts = [((thisx, thisymin), (thisx, thisymax)) + for thisx, (thisymin, thisymax) in zip(x, Y)] + #print 'creating line collection' + coll = mcoll.LineCollection(verts, colors=colors, + linestyles=linestyles, label=label) + ax.add_collection(coll) + coll.update(kwargs) + + if len(x) > 0: + minx = min(x) + maxx = max(x) + + miny = min(min(ymin), min(ymax)) + maxy = max(max(ymin), max(ymax)) + + corners = (minx, miny), (maxx, maxy) + ax.update_datalim(corners) + ax.autoscale_view() + + return coll + + +def axhspan(ax, ymin, ymax, xmin=0, xmax=1, **kwargs): + """ + Add a horizontal span (rectangle) across the axis. + + Call signature:: + + axhspan(ymin, ymax, xmin=0, xmax=1, **kwargs) + + *y* coords are in data units and *x* coords are in axes (relative + 0-1) units. + + Draw a horizontal span (rectangle) from *ymin* to *ymax*. + With the default values of *xmin* = 0 and *xmax* = 1, this + always spans the xrange, regardless of the xlim settings, even + if you change them, e.g., with the :meth:`set_xlim` command. + That is, the horizontal extent is in axes coords: 0=left, + 0.5=middle, 1.0=right but the *y* location is in data + coordinates. + + Return value is a :class:`matplotlib.patches.Polygon` + instance. + + Examples: + + * draw a gray rectangle from *y* = 0.25-0.75 that spans the + horizontal extent of the axes:: + + >>> axhspan(0.25, 0.75, facecolor='0.5', alpha=0.5) + + Valid kwargs are :class:`~matplotlib.patches.Polygon` properties: + + %(Polygon)s + + **Example:** + + .. plot:: mpl_examples/pylab_examples/axhspan_demo.py + + """ + trans = mtransforms.blended_transform_factory( + ax.transAxes, ax.transData) + + # process the unit information + ax._process_unit_info([xmin, xmax], [ymin, ymax], kwargs=kwargs) + + # first we need to strip away the units + xmin, xmax = ax.convert_xunits([xmin, xmax]) + ymin, ymax = ax.convert_yunits([ymin, ymax]) + + verts = (xmin, ymin), (xmin, ymax), (xmax, ymax), (xmax, ymin) + p = mpatches.Polygon(verts, **kwargs) + p.set_transform(trans) + ax.add_patch(p) + ax.autoscale_view(scalex=False) + return p + +def axvspan(ax, xmin, xmax, ymin=0, ymax=1, **kwargs): + """ + Add a vertical span (rectangle) across the axes. + + Call signature:: + + axvspan(xmin, xmax, ymin=0, ymax=1, **kwargs) + + *x* coords are in data units and *y* coords are in axes (relative + 0-1) units. + + Draw a vertical span (rectangle) from *xmin* to *xmax*. With + the default values of *ymin* = 0 and *ymax* = 1, this always + spans the yrange, regardless of the ylim settings, even if you + change them, e.g., with the :meth:`set_ylim` command. That is, + the vertical extent is in axes coords: 0=bottom, 0.5=middle, + 1.0=top but the *y* location is in data coordinates. + + Return value is the :class:`matplotlib.patches.Polygon` + instance. + + Examples: + + * draw a vertical green translucent rectangle from x=1.25 to 1.55 that + spans the yrange of the axes:: + + >>> axvspan(1.25, 1.55, facecolor='g', alpha=0.5) + + Valid kwargs are :class:`~matplotlib.patches.Polygon` + properties: + + %(Polygon)s + + .. seealso:: + + :meth:`axhspan` + for example plot and source code + """ + trans = mtransforms.blended_transform_factory( + ax.transData, ax.transAxes) + + # process the unit information + ax._process_unit_info([xmin, xmax], [ymin, ymax], kwargs=kwargs) + + # first we need to strip away the units + xmin, xmax = ax.convert_xunits([xmin, xmax]) + ymin, ymax = ax.convert_yunits([ymin, ymax]) + + verts = [(xmin, ymin), (xmin, ymax), (xmax, ymax), (xmax, ymin)] + p = mpatches.Polygon(verts, **kwargs) + p.set_transform(trans) + ax.add_patch(p) + ax.autoscale_view(scaley=False) + return p + + From 0741f5763694cadf39581ceb040edc0d9f24ef68 Mon Sep 17 00:00:00 2001 From: Nelle Varoquaux Date: Tue, 16 Jul 2013 19:13:23 +0200 Subject: [PATCH 06/12] PEP8 axes._lines was not pep8 compliant --- lib/matplotlib/axes/_lines.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/matplotlib/axes/_lines.py b/lib/matplotlib/axes/_lines.py index dde03638795d..b2f6d2b8ad54 100644 --- a/lib/matplotlib/axes/_lines.py +++ b/lib/matplotlib/axes/_lines.py @@ -382,6 +382,7 @@ def axhspan(ax, ymin, ymax, xmin=0, xmax=1, **kwargs): ax.autoscale_view(scalex=False) return p + def axvspan(ax, xmin, xmax, ymin=0, ymax=1, **kwargs): """ Add a vertical span (rectangle) across the axes. @@ -436,5 +437,3 @@ def axvspan(ax, xmin, xmax, ymin=0, ymax=1, **kwargs): ax.add_patch(p) ax.autoscale_view(scaley=False) return p - - From e48d4b78888e043dd13f38d4b84f891585ec322b Mon Sep 17 00:00:00 2001 From: Nelle Varoquaux Date: Tue, 16 Jul 2013 19:27:27 +0200 Subject: [PATCH 07/12] FIX missing imports --- lib/matplotlib/axes/_lines.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/matplotlib/axes/_lines.py b/lib/matplotlib/axes/_lines.py index b2f6d2b8ad54..f072e6fcbe1d 100644 --- a/lib/matplotlib/axes/_lines.py +++ b/lib/matplotlib/axes/_lines.py @@ -5,6 +5,7 @@ from matplotlib import transforms as mtransforms from matplotlib import lines as mlines +from matplotlib import patches as mpatches from matplotlib import collections as mcoll from matplotlib.cbook import iterable @@ -239,8 +240,6 @@ def hlines(ax, y, xmin, xmax, colors='k', linestyles='solid', def vlines(ax, x, ymin, ymax, colors='k', linestyles='solid', label='', **kwargs): """ - Plot vertical lines. - Plot vertical lines at each `x` from `ymin` to `ymax`. Parameters From eedb3c36383a00a46d5eaaa4983e50a52518fd20 Mon Sep 17 00:00:00 2001 From: Nelle Varoquaux Date: Thu, 18 Jul 2013 15:14:12 +0200 Subject: [PATCH 08/12] MEP10 moved axhspan's doc to numpydoc --- lib/matplotlib/axes/_lines.py | 45 ++++++++++++++++++----------------- 1 file changed, 23 insertions(+), 22 deletions(-) diff --git a/lib/matplotlib/axes/_lines.py b/lib/matplotlib/axes/_lines.py index f072e6fcbe1d..8c6467a38886 100644 --- a/lib/matplotlib/axes/_lines.py +++ b/lib/matplotlib/axes/_lines.py @@ -328,38 +328,39 @@ def vlines(ax, x, ymin, ymax, colors='k', linestyles='solid', def axhspan(ax, ymin, ymax, xmin=0, xmax=1, **kwargs): """ - Add a horizontal span (rectangle) across the axis. + Draws a horizontal span (rectangle) across the axis. - Call signature:: - - axhspan(ymin, ymax, xmin=0, xmax=1, **kwargs) + Parameters + ---------- - *y* coords are in data units and *x* coords are in axes (relative - 0-1) units. + ymin, ymax : scalars + Coordinates in data units of the horizontal span. - Draw a horizontal span (rectangle) from *ymin* to *ymax*. - With the default values of *xmin* = 0 and *xmax* = 1, this - always spans the xrange, regardless of the xlim settings, even - if you change them, e.g., with the :meth:`set_xlim` command. - That is, the horizontal extent is in axes coords: 0=left, - 0.5=middle, 1.0=right but the *y* location is in data - coordinates. + xmin, xmax: scalars, optional, default: 0, 1 + Coordinates in relative axes coordinates. 0 is bottom, 0.5 is the + middle and 1 is top. This always spans the xrange, regardless of the + xlim settings (even if you change them with the `set_xlim` method). - Return value is a :class:`matplotlib.patches.Polygon` - instance. + Returns + ------- + polygon : `~matplotlib.patches.Polygon` - Examples: + Notes + ----- + Valid kwargs are :class:`~matplotlib.patches.Polygon` properties: - * draw a gray rectangle from *y* = 0.25-0.75 that spans the - horizontal extent of the axes:: + %(Polygon)s - >>> axhspan(0.25, 0.75, facecolor='0.5', alpha=0.5) + Examples + -------- - Valid kwargs are :class:`~matplotlib.patches.Polygon` properties: + - draws a gray rectangle from `y = 0.25-0.75` that spans the horizontal + extent of the axes:: - %(Polygon)s + >>> from matplotlib import pyplot as plt + >>> plt.axhspan(0.25, 0.75, facecolor='0.5', alpha=0.5) - **Example:** + - a more complex example: .. plot:: mpl_examples/pylab_examples/axhspan_demo.py From a9e035bff6913146705cb1c9eacaa27848ac9487 Mon Sep 17 00:00:00 2001 From: Nelle Varoquaux Date: Thu, 18 Jul 2013 15:17:37 +0200 Subject: [PATCH 09/12] MEP10 moved axvspan's doc to numpydoc --- lib/matplotlib/axes/_lines.py | 47 ++++++++++++++++------------------- 1 file changed, 21 insertions(+), 26 deletions(-) diff --git a/lib/matplotlib/axes/_lines.py b/lib/matplotlib/axes/_lines.py index 8c6467a38886..322a0e04266e 100644 --- a/lib/matplotlib/axes/_lines.py +++ b/lib/matplotlib/axes/_lines.py @@ -385,41 +385,36 @@ def axhspan(ax, ymin, ymax, xmin=0, xmax=1, **kwargs): def axvspan(ax, xmin, xmax, ymin=0, ymax=1, **kwargs): """ - Add a vertical span (rectangle) across the axes. + Draws a vertical span (rectangle) across the axis. - Call signature:: - - axvspan(xmin, xmax, ymin=0, ymax=1, **kwargs) - - *x* coords are in data units and *y* coords are in axes (relative - 0-1) units. - - Draw a vertical span (rectangle) from *xmin* to *xmax*. With - the default values of *ymin* = 0 and *ymax* = 1, this always - spans the yrange, regardless of the ylim settings, even if you - change them, e.g., with the :meth:`set_ylim` command. That is, - the vertical extent is in axes coords: 0=bottom, 0.5=middle, - 1.0=top but the *y* location is in data coordinates. - - Return value is the :class:`matplotlib.patches.Polygon` - instance. + Parameters + ---------- - Examples: + xmin, xmax : scalars + Coordinates in data units of the vertical span. - * draw a vertical green translucent rectangle from x=1.25 to 1.55 that - spans the yrange of the axes:: + ymin, ymax: scalars, optional, default: 0, 1 + Coordinates in relative axes coordinates. 0 is bottom, 0.5 is the + middle and 1 is top. This always spans the xrange, regardless of the + xlim settings (even if you change them with the `set_xlim` method). - >>> axvspan(1.25, 1.55, facecolor='g', alpha=0.5) + Returns + ------- + polygon : `~matplotlib.patches.Polygon` - Valid kwargs are :class:`~matplotlib.patches.Polygon` - properties: + Notes + ----- + Valid kwargs are :class:`~matplotlib.patches.Polygon` properties: %(Polygon)s - .. seealso:: + Examples + --------- + - draws a vertical green translucent rectangle from x=1.25 to 1.55 that + spans the yrange of the axes:: - :meth:`axhspan` - for example plot and source code + >>> from matplotlib import pyplot as plt + >>> plt.axvspan(1.25, 1.55, facecolor='g', alpha=0.5) """ trans = mtransforms.blended_transform_factory( ax.transData, ax.transAxes) From dc5b52a4be04a2e75665ba8088814ba75afbe517 Mon Sep 17 00:00:00 2001 From: Nelle Varoquaux Date: Fri, 19 Jul 2013 10:29:06 +0200 Subject: [PATCH 10/12] ENH the axes class now uses mixins. Lines and Spans function are set to be in a class. Axes now inherits from _AxesBase, and LinesAndSpans is a mixin --- lib/matplotlib/axes/_axes.py | 32 +- lib/matplotlib/axes/_lines.py | 649 +++++++++++++++++----------------- 2 files changed, 328 insertions(+), 353 deletions(-) diff --git a/lib/matplotlib/axes/_axes.py b/lib/matplotlib/axes/_axes.py index 5ce38965da28..5600bd58195a 100644 --- a/lib/matplotlib/axes/_axes.py +++ b/lib/matplotlib/axes/_axes.py @@ -43,7 +43,7 @@ # The axes module contains all the wrappers to plotting functions. # All the other methods should go in the _AxesBase class. -class Axes(_AxesBase): +class Axes(_AxesBase, _lines.LinesAndSpans): """ The :class:`Axes` contains most of the figure elements: :class:`~matplotlib.axis.Axis`, :class:`~matplotlib.axis.Tick`, @@ -57,6 +57,7 @@ class Axes(_AxesBase): 'ylim_changed' and the callback will be called with func(*ax*) where *ax* is the :class:`Axes` instance. """ + ### Labelling, legend and texts def get_title(self, loc="center"): @@ -590,35 +591,6 @@ def annotate(self, *args, **kwargs): a._remove_method = lambda h: self.texts.remove(h) return a - #### Lines and spans - - def axhline(self, y=0, xmin=0, xmax=1, **kwargs): - return _lines.axhline(self, y=0, xmin=0, xmax=1, **kwargs) - axhline.__doc__ = _lines.axhline.__doc__ - - def axvline(self, x=0, ymin=0, ymax=1, **kwargs): - return _lines.axvline(self, x=0, ymin=0, ymax=1, **kwargs) - axvline.__doc__ = _lines.axvline.__doc__ - - def axhspan(self, ymin, ymax, xmin=0, xmax=1, **kwargs): - return _lines.axhspan(self, ymin, ymax, xmin=0, xmax=1, **kwargs) - axhspan.__doc__ = _lines.axhspan.__doc__ - - def axvspan(self, xmin, xmax, ymin=0, ymax=1, **kwargs): - return _lines.axvspan(self, xmin, xmax, ymin=0, ymax=1, **kwargs) - axvspan.__doc__ = _lines.axvspan.__doc__ - - def hlines(self, y, xmin, xmax, colors='k', linestyles='solid', - label='', **kwargs): - return _lines.hlines(self, y, xmin, xmax, colors='k', - linestyles='solid', label='', **kwargs) - hlines.__doc__ = _lines.hlines.__doc__ - - def vlines(self, x, ymin, ymax, colors='k', linestyles='solid', - label='', **kwargs): - return _lines.vlines.__doc__ - vlines.__doc__ = _lines.vlines.__doc__ - @docstring.dedent_interpd def eventplot(self, positions, orientation='horizontal', lineoffsets=1, linelengths=1, linewidths=None, colors=None, diff --git a/lib/matplotlib/axes/_lines.py b/lib/matplotlib/axes/_lines.py index 322a0e04266e..45a2a1018574 100644 --- a/lib/matplotlib/axes/_lines.py +++ b/lib/matplotlib/axes/_lines.py @@ -8,427 +8,430 @@ from matplotlib import patches as mpatches from matplotlib import collections as mcoll from matplotlib.cbook import iterable +from ._base import _AxesBase -def axhline(ax, y=0, xmin=0, xmax=1, **kwargs): - """ - Add a horizontal line across the axis. +class LinesAndSpans(_AxesBase): + def axhline(self, y=0, xmin=0, xmax=1, **kwargs): + """ + Add a horizontal line across the axis. - Parameters - ---------- - y : scalar, optional, default: 0 - y position in data coordinates of the horizontal line. + Parameters + ---------- + y : scalar, optional, default: 0 + y position in data coordinates of the horizontal line. - xmin : scalar, optional, default: 0 - Should be between 0 and 1, 0 being the far left of the plot, 1 the - far right of the plot. + xmin : scalar, optional, default: 0 + Should be between 0 and 1, 0 being the far left of the plot, 1 the + far right of the plot. - xmax : scalar, optional, default: 1 - Should be between 0 and 1, 0 being the far left of the plot, 1 the - far right of the plot. + xmax : scalar, optional, default: 1 + Should be between 0 and 1, 0 being the far left of the plot, 1 the + far right of the plot. - Returns - ------- - `~matplotlib.lines.Line2D` + Returns + ------- + `~matplotlib.lines.Line2D` - Notes - ----- - kwargs are the same as kwargs to plot, and can be - used to control the line properties. e.g., + Notes + ----- + kwargs are the same as kwargs to plot, and can be + used to control the line properties. e.g., - Examples - -------- + Examples + -------- - * draw a thick red hline at 'y' = 0 that spans the xrange:: + * draw a thick red hline at 'y' = 0 that spans the xrange:: - >>> axhline(linewidth=4, color='r') + >>> from matplotlib import pyplot as plt + >>> plt.axhline(linewidth=4, color='r') - * draw a default hline at 'y' = 1 that spans the xrange:: + * draw a default hline at 'y' = 1 that spans the xrange:: - >>> axhline(y=1) + >>> from matplotlib import pyplot as plt + >>> plt.axhline(y=1) - * draw a default hline at 'y' = .5 that spans the the middle half of - the xrange:: + * draw a default hline at 'y' = .5 that spans the the middle half of + the xrange:: - >>> axhline(y=.5, xmin=0.25, xmax=0.75) + >>> from matplotlib import pyplot as plt + >>> plt.axhline(y=.5, xmin=0.25, xmax=0.75) - Valid kwargs are :class:`~matplotlib.lines.Line2D` properties, - with the exception of 'transform': + Valid kwargs are :class:`~matplotlib.lines.Line2D` properties, + with the exception of 'transform': - %(Line2D)s + %(Line2D)s - See also - -------- - `axhspan` for example plot and source code - """ + See also + -------- + `axhspan` for example plot and source code + """ - if "transform" in kwargs: - raise ValueError( - "'transform' is not allowed as a kwarg;" - + "axhline generates its own transform.") - ymin, ymax = ax.get_ybound() + if "transform" in kwargs: + raise ValueError( + "'transform' is not allowed as a kwarg;" + + "axhline generates its own transform.") + ymin, ymax = self.get_ybound() - # We need to strip away the units for comparison with - # non-unitized bounds - ax._process_unit_info(ydata=y, kwargs=kwargs) - yy = ax.convert_yunits(y) - scaley = (yy < ymin) or (yy > ymax) + # We need to strip away the units for comparison with + # non-unitized bounds + self._process_unit_info(ydata=y, kwargs=kwargs) + yy = self.convert_yunits(y) + scaley = (yy < ymin) or (yy > ymax) - trans = mtransforms.blended_transform_factory( - ax.transAxes, ax.transData) - l = mlines.Line2D([xmin, xmax], [y, y], transform=trans, **kwargs) - ax.add_line(l) - ax.autoscale_view(scalex=False, scaley=scaley) - return l + trans = mtransforms.blended_transform_factory( + self.transAxes, self.transData) + l = mlines.Line2D([xmin, xmax], [y, y], transform=trans, **kwargs) + self.add_line(l) + self.autoscale_view(scalex=False, scaley=scaley) + return l + def axvline(self, x=0, ymin=0, ymax=1, **kwargs): + """ + Add a vertical line across the axes. -def axvline(ax, x=0, ymin=0, ymax=1, **kwargs): - """ - Add a vertical line across the axes. + Parameters + ---------- + x : scalar, optional, default: 0 + y position in data coordinates of the vertical line. - Parameters - ---------- - x : scalar, optional, default: 0 - y position in data coordinates of the vertical line. + ymin : scalar, optional, default: 0 + Should be between 0 and 1, 0 being the far left of the plot, 1 the + far right of the plot. - ymin : scalar, optional, default: 0 - Should be between 0 and 1, 0 being the far left of the plot, 1 the - far right of the plot. + ymax : scalar, optional, default: 1 + Should be between 0 and 1, 0 being the far left of the plot, 1 the + far right of the plot. - ymax : scalar, optional, default: 1 - Should be between 0 and 1, 0 being the far left of the plot, 1 the - far right of the plot. + Returns + ------- + `~matplotlib.lines.Line2D` - Returns - ------- - `~matplotlib.lines.Line2D` + Examples + --------- + * draw a thick red vline at *x* = 0 that spans the yrange:: + >>> from matplotlib import pyplot as plt + >>> plt.axvline(linewidth=4, color='r') - Examples - --------- - * draw a thick red vline at *x* = 0 that spans the yrange:: + * draw a default vline at *x* = 1 that spans the yrange:: - >>> axvline(linewidth=4, color='r') + >>> from matplotlib import pyplot as plt + >>> plt.axvline(x=1) - * draw a default vline at *x* = 1 that spans the yrange:: + * draw a default vline at *x* = .5 that spans the the middle half of + the yrange:: - >>> axvline(x=1) + >>> from matplotlib import pyplot as plt + >>> plt.axvline(x=.5, ymin=0.25, ymax=0.75) - * draw a default vline at *x* = .5 that spans the the middle half of - the yrange:: + Valid kwargs are :class:`~matplotlib.lines.Line2D` properties, + with the exception of 'transform': - >>> axvline(x=.5, ymin=0.25, ymax=0.75) + %(Line2D)s - Valid kwargs are :class:`~matplotlib.lines.Line2D` properties, - with the exception of 'transform': + See also + -------- - %(Line2D)s + `axhspan` for example plot and source code + """ - See also - -------- + if "transform" in kwargs: + raise ValueError( + "'transform' is not allowed as a kwarg;" + + "axvline generates its own transform.") + xmin, xmax = self.get_xbound() - `axhspan` for example plot and source code - """ + # We need to strip away the units for comparison with + # non-unitized bounds + self._process_unit_info(xdata=x, kwargs=kwargs) + xx = self.convert_xunits(x) + scalex = (xx < xmin) or (xx > xmax) - if "transform" in kwargs: - raise ValueError( - "'transform' is not allowed as a kwarg;" - + "axvline generates its own transform.") - xmin, xmax = ax.get_xbound() + trans = mtransforms.blended_transform_factory( + self.transData, self.transAxes) + l = mlines.Line2D([x, x], [ymin, ymax], transform=trans, **kwargs) + self.add_line(l) + self.autoscale_view(scalex=scalex, scaley=False) + return l + + def hlines(self, y, xmin, xmax, colors='k', linestyles='solid', + label='', **kwargs): + """ + Plot horizontal lines at each `y` from `xmin` to `xmax`. - # We need to strip away the units for comparison with - # non-unitized bounds - ax._process_unit_info(xdata=x, kwargs=kwargs) - xx = ax.convert_xunits(x) - scalex = (xx < xmin) or (xx > xmax) + Parameters + ---------- + y : scalar or sequence of scalar + y-indexes where to plot the lines. - trans = mtransforms.blended_transform_factory( - ax.transData, ax.transAxes) - l = mlines.Line2D([x, x], [ymin, ymax], transform=trans, **kwargs) - ax.add_line(l) - ax.autoscale_view(scalex=scalex, scaley=False) - return l + xmin, xmax : scalar or 1D array_like + Respective beginning and end of each line. If scalars are + provided, all lines will have same length. + colors : array_like of colors, optional, default: 'k' -def hlines(ax, y, xmin, xmax, colors='k', linestyles='solid', - label='', **kwargs): - """ - Plot horizontal lines at each `y` from `xmin` to `xmax`. + linestyles : ['solid' | 'dashed' | 'dashdot' | 'dotted'], optional - Parameters - ---------- - y : scalar or sequence of scalar - y-indexes where to plot the lines. + label : string, optional, default: '' - xmin, xmax : scalar or 1D array_like - Respective beginning and end of each line. If scalars are - provided, all lines will have same length. + Returns + ------- + lines : `~matplotlib.collections.LineCollection` - colors : array_like of colors, optional, default: 'k' + Other parameters + ---------------- + kwargs : `~matplotlib.collections.LineCollection` properties. - linestyles : ['solid' | 'dashed' | 'dashdot' | 'dotted'], optional + See also + -------- + vlines : vertical lines - label : string, optional, default: '' + Examples + -------- + .. plot:: mpl_examples/pylab_examples/vline_hline_demo.py - Returns - ------- - lines : `~matplotlib.collections.LineCollection` + """ - Other parameters - ---------------- - kwargs : `~matplotlib.collections.LineCollection` properties. + # We do the conversion first since not all unitized data is uniform + # process the unit information + self._process_unit_info([xmin, xmax], y, kwargs=kwargs) + y = self.convert_yunits(y) + xmin = self.convert_xunits(xmin) + xmax = self.convert_xunits(xmax) - See also - -------- - vlines : vertical lines + if not iterable(y): + y = [y] + if not iterable(xmin): + xmin = [xmin] + if not iterable(xmax): + xmax = [xmax] - Examples - -------- - .. plot:: mpl_examples/pylab_examples/vline_hline_demo.py + y = np.asarray(y) + xmin = np.asarray(xmin) + xmax = np.asarray(xmax) - """ + if len(xmin) == 1: + xmin = np.resize(xmin, y.shape) + if len(xmax) == 1: + xmax = np.resize(xmax, y.shape) - # We do the conversion first since not all unitized data is uniform - # process the unit information - ax._process_unit_info([xmin, xmax], y, kwargs=kwargs) - y = ax.convert_yunits(y) - xmin = ax.convert_xunits(xmin) - xmax = ax.convert_xunits(xmax) + if len(xmin) != len(y): + raise ValueError('xmin and y are unequal sized sequences') + if len(xmax) != len(y): + raise ValueError('xmax and y are unequal sized sequences') + + verts = [((thisxmin, thisy), (thisxmax, thisy)) + for thisxmin, thisxmax, thisy in zip(xmin, xmax, y)] + coll = mcoll.LineCollection(verts, colors=colors, + linestyles=linestyles, label=label) + self.add_collection(coll) + coll.update(kwargs) - if not iterable(y): - y = [y] - if not iterable(xmin): - xmin = [xmin] - if not iterable(xmax): - xmax = [xmax] + if len(y) > 0: + minx = min(xmin.min(), xmax.min()) + maxx = max(xmin.max(), xmax.max()) + miny = y.min() + maxy = y.max() + + corners = (minx, miny), (maxx, maxy) + + self.update_datalim(corners) + self.autoscale_view() + + return coll + + def vlines(self, x, ymin, ymax, colors='k', linestyles='solid', + label='', **kwargs): + """ + Plot vertical lines at each `x` from `ymin` to `ymax`. + + Parameters + ---------- + x : scalar or 1D array_like + x-indexes where to plot the lines. + + xmin, xmax : scalar or 1D array_like + Respective beginning and end of each line. If scalars are + provided, all lines will have same length. - y = np.asarray(y) - xmin = np.asarray(xmin) - xmax = np.asarray(xmax) + colors : array_like of colors, optional, default: 'k' - if len(xmin) == 1: - xmin = np.resize(xmin, y.shape) - if len(xmax) == 1: - xmax = np.resize(xmax, y.shape) + linestyles : ['solid' | 'dashed' | 'dashdot' | 'dotted'], optional - if len(xmin) != len(y): - raise ValueError('xmin and y are unequal sized sequences') - if len(xmax) != len(y): - raise ValueError('xmax and y are unequal sized sequences') + label : string, optional, default: '' - verts = [((thisxmin, thisy), (thisxmax, thisy)) - for thisxmin, thisxmax, thisy in zip(xmin, xmax, y)] - coll = mcoll.LineCollection(verts, colors=colors, - linestyles=linestyles, label=label) - ax.add_collection(coll) - coll.update(kwargs) + Returns + ------- + lines : `~matplotlib.collections.LineCollection` - if len(y) > 0: - minx = min(xmin.min(), xmax.min()) - maxx = max(xmin.max(), xmax.max()) - miny = y.min() - maxy = y.max() + Other parameters + ---------------- + kwargs : `~matplotlib.collections.LineCollection` properties. - corners = (minx, miny), (maxx, maxy) + See also + -------- + hlines : horizontal lines - ax.update_datalim(corners) - ax.autoscale_view() + Examples + --------- + .. plot:: mpl_examples/pylab_examples/vline_hline_demo.py - return coll + """ + self._process_unit_info(xdata=x, ydata=[ymin, ymax], kwargs=kwargs) -def vlines(ax, x, ymin, ymax, colors='k', linestyles='solid', - label='', **kwargs): - """ - Plot vertical lines at each `x` from `ymin` to `ymax`. + # We do the conversion first since not all unitized data is uniform + x = self.convert_xunits(x) + ymin = self.convert_yunits(ymin) + ymax = self.convert_yunits(ymax) - Parameters - ---------- - x : scalar or 1D array_like - x-indexes where to plot the lines. + if not iterable(x): + x = [x] + if not iterable(ymin): + ymin = [ymin] + if not iterable(ymax): + ymax = [ymax] - xmin, xmax : scalar or 1D array_like - Respective beginning and end of each line. If scalars are - provided, all lines will have same length. + x = np.asarray(x) + ymin = np.asarray(ymin) + ymax = np.asarray(ymax) + if len(ymin) == 1: + ymin = np.resize(ymin, x.shape) + if len(ymax) == 1: + ymax = np.resize(ymax, x.shape) - colors : array_like of colors, optional, default: 'k' + if len(ymin) != len(x): + raise ValueError('ymin and x are unequal sized sequences') + if len(ymax) != len(x): + raise ValueError('ymax and x are unequal sized sequences') - linestyles : ['solid' | 'dashed' | 'dashdot' | 'dotted'], optional + Y = np.array([ymin, ymax]).T - label : string, optional, default: '' + verts = [((thisx, thisymin), (thisx, thisymax)) + for thisx, (thisymin, thisymax) in zip(x, Y)] + coll = mcoll.LineCollection(verts, colors=colors, + linestyles=linestyles, label=label) + self.add_collection(coll) + coll.update(kwargs) - Returns - ------- - lines : `~matplotlib.collections.LineCollection` + if len(x) > 0: + minx = min(x) + maxx = max(x) - Other parameters - ---------------- - kwargs : `~matplotlib.collections.LineCollection` properties. + miny = min(min(ymin), min(ymax)) + maxy = max(max(ymin), max(ymax)) - See also - -------- - hlines : horizontal lines + corners = (minx, miny), (maxx, maxy) + self.update_datalim(corners) + self.autoscale_view() - Examples - --------- - .. plot:: mpl_examples/pylab_examples/vline_hline_demo.py + return coll - """ + def axhspan(self, ymin, ymax, xmin=0, xmax=1, **kwargs): + """ + Draws a horizontal span (rectangle) across the axis. - ax._process_unit_info(xdata=x, ydata=[ymin, ymax], kwargs=kwargs) + Parameters + ---------- - # We do the conversion first since not all unitized data is uniform - x = ax.convert_xunits(x) - ymin = ax.convert_yunits(ymin) - ymax = ax.convert_yunits(ymax) + ymin, ymax : scalars + Coordinates in data units of the horizontal span. - if not iterable(x): - x = [x] - if not iterable(ymin): - ymin = [ymin] - if not iterable(ymax): - ymax = [ymax] + xmin, xmax: scalars, optional, default: 0, 1 + Coordinates in relative axes coordinates. 0 is bottom, 0.5 is the + middle and 1 is top. This always spans the xrange, regardless of + the xlim settings (even if you change them with the `set_xlim` + method). - x = np.asarray(x) - ymin = np.asarray(ymin) - ymax = np.asarray(ymax) - if len(ymin) == 1: - ymin = np.resize(ymin, x.shape) - if len(ymax) == 1: - ymax = np.resize(ymax, x.shape) + Returns + ------- + polygon : `~matplotlib.patches.Polygon` - if len(ymin) != len(x): - raise ValueError('ymin and x are unequal sized sequences') - if len(ymax) != len(x): - raise ValueError('ymax and x are unequal sized sequences') + Notes + ----- + Valid kwargs are :class:`~matplotlib.patches.Polygon` properties: - Y = np.array([ymin, ymax]).T + %(Polygon)s - verts = [((thisx, thisymin), (thisx, thisymax)) - for thisx, (thisymin, thisymax) in zip(x, Y)] - #print 'creating line collection' - coll = mcoll.LineCollection(verts, colors=colors, - linestyles=linestyles, label=label) - ax.add_collection(coll) - coll.update(kwargs) + Examples + -------- - if len(x) > 0: - minx = min(x) - maxx = max(x) + - draws a gray rectangle from `y = 0.25-0.75` that spans the horizontal + extent of the axes:: - miny = min(min(ymin), min(ymax)) - maxy = max(max(ymin), max(ymax)) + >>> from matplotlib import pyplot as plt + >>> plt.axhspan(0.25, 0.75, facecolor='0.5', alpha=0.5) - corners = (minx, miny), (maxx, maxy) - ax.update_datalim(corners) - ax.autoscale_view() + - a more complex example: - return coll + .. plot:: mpl_examples/pylab_examples/axhspan_demo.py + """ + trans = mtransforms.blended_transform_factory( + self.transAxes, self.transData) -def axhspan(ax, ymin, ymax, xmin=0, xmax=1, **kwargs): - """ - Draws a horizontal span (rectangle) across the axis. + # process the unit information + self._process_unit_info([xmin, xmax], [ymin, ymax], kwargs=kwargs) - Parameters - ---------- + # first we need to strip away the units + xmin, xmax = self.convert_xunits([xmin, xmax]) + ymin, ymax = self.convert_yunits([ymin, ymax]) - ymin, ymax : scalars - Coordinates in data units of the horizontal span. + verts = (xmin, ymin), (xmin, ymax), (xmax, ymax), (xmax, ymin) + p = mpatches.Polygon(verts, **kwargs) + p.set_transform(trans) + self.add_patch(p) + self.autoscale_view(scalex=False) + return p - xmin, xmax: scalars, optional, default: 0, 1 - Coordinates in relative axes coordinates. 0 is bottom, 0.5 is the - middle and 1 is top. This always spans the xrange, regardless of the - xlim settings (even if you change them with the `set_xlim` method). + def axvspan(self, xmin, xmax, ymin=0, ymax=1, **kwargs): + """ + Draws a vertical span (rectangle) across the axis. - Returns - ------- - polygon : `~matplotlib.patches.Polygon` + Parameters + ---------- - Notes - ----- - Valid kwargs are :class:`~matplotlib.patches.Polygon` properties: + xmin, xmax : scalars + Coordinates in data units of the vertical span. - %(Polygon)s + ymin, ymax: scalars, optional, default: 0, 1 + Coordinates in relative axes coordinates. 0 is bottom, 0.5 is the + middle and 1 is top. This always spans the xrange, regardless of + the xlim settings (even if you change them with the `set_xlim` + method). - Examples - -------- + Returns + ------- + polygon : `~matplotlib.patches.Polygon` - - draws a gray rectangle from `y = 0.25-0.75` that spans the horizontal - extent of the axes:: + Notes + ----- + Valid kwargs are :class:`~matplotlib.patches.Polygon` properties: - >>> from matplotlib import pyplot as plt - >>> plt.axhspan(0.25, 0.75, facecolor='0.5', alpha=0.5) + %(Polygon)s - - a more complex example: + Examples + --------- + - draws a vertical green translucent rectangle from x=1.25 to 1.55 that + spans the yrange of the axes:: - .. plot:: mpl_examples/pylab_examples/axhspan_demo.py + >>> from matplotlib import pyplot as plt + >>> plt.axvspan(1.25, 1.55, facecolor='g', alpha=0.5) + """ + trans = mtransforms.blended_transform_factory( + self.transData, self.transAxes) - """ - trans = mtransforms.blended_transform_factory( - ax.transAxes, ax.transData) + # process the unit information + self._process_unit_info([xmin, xmax], [ymin, ymax], kwargs=kwargs) - # process the unit information - ax._process_unit_info([xmin, xmax], [ymin, ymax], kwargs=kwargs) + # first we need to strip away the units + xmin, xmax = self.convert_xunits([xmin, xmax]) + ymin, ymax = self.convert_yunits([ymin, ymax]) - # first we need to strip away the units - xmin, xmax = ax.convert_xunits([xmin, xmax]) - ymin, ymax = ax.convert_yunits([ymin, ymax]) - - verts = (xmin, ymin), (xmin, ymax), (xmax, ymax), (xmax, ymin) - p = mpatches.Polygon(verts, **kwargs) - p.set_transform(trans) - ax.add_patch(p) - ax.autoscale_view(scalex=False) - return p - - -def axvspan(ax, xmin, xmax, ymin=0, ymax=1, **kwargs): - """ - Draws a vertical span (rectangle) across the axis. - - Parameters - ---------- - - xmin, xmax : scalars - Coordinates in data units of the vertical span. - - ymin, ymax: scalars, optional, default: 0, 1 - Coordinates in relative axes coordinates. 0 is bottom, 0.5 is the - middle and 1 is top. This always spans the xrange, regardless of the - xlim settings (even if you change them with the `set_xlim` method). - - Returns - ------- - polygon : `~matplotlib.patches.Polygon` - - Notes - ----- - Valid kwargs are :class:`~matplotlib.patches.Polygon` properties: - - %(Polygon)s - - Examples - --------- - - draws a vertical green translucent rectangle from x=1.25 to 1.55 that - spans the yrange of the axes:: - - >>> from matplotlib import pyplot as plt - >>> plt.axvspan(1.25, 1.55, facecolor='g', alpha=0.5) - """ - trans = mtransforms.blended_transform_factory( - ax.transData, ax.transAxes) - - # process the unit information - ax._process_unit_info([xmin, xmax], [ymin, ymax], kwargs=kwargs) - - # first we need to strip away the units - xmin, xmax = ax.convert_xunits([xmin, xmax]) - ymin, ymax = ax.convert_yunits([ymin, ymax]) - - verts = [(xmin, ymin), (xmin, ymax), (xmax, ymax), (xmax, ymin)] - p = mpatches.Polygon(verts, **kwargs) - p.set_transform(trans) - ax.add_patch(p) - ax.autoscale_view(scaley=False) - return p + verts = [(xmin, ymin), (xmin, ymax), (xmax, ymax), (xmax, ymin)] + p = mpatches.Polygon(verts, **kwargs) + p.set_transform(trans) + self.add_patch(p) + self.autoscale_view(scaley=False) + return p From bfcd30a4667d3ed683d072e80f4ff65199cf9133 Mon Sep 17 00:00:00 2001 From: Nelle Varoquaux Date: Fri, 19 Jul 2013 10:33:21 +0200 Subject: [PATCH 11/12] FIX LinesAndSpans should not inherit from _AxesBase --- lib/matplotlib/axes/_lines.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/matplotlib/axes/_lines.py b/lib/matplotlib/axes/_lines.py index 45a2a1018574..42eaa623b724 100644 --- a/lib/matplotlib/axes/_lines.py +++ b/lib/matplotlib/axes/_lines.py @@ -11,7 +11,7 @@ from ._base import _AxesBase -class LinesAndSpans(_AxesBase): +class LinesAndSpans(object): def axhline(self, y=0, xmin=0, xmax=1, **kwargs): """ Add a horizontal line across the axis. From 9982490250e40882a14c0ee06e699fd2357d7878 Mon Sep 17 00:00:00 2001 From: Nelle Varoquaux Date: Fri, 19 Jul 2013 13:46:22 +0200 Subject: [PATCH 12/12] FIX removed unusued import in the axes._lines module --- lib/matplotlib/axes/_lines.py | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/matplotlib/axes/_lines.py b/lib/matplotlib/axes/_lines.py index 42eaa623b724..bcc0d987cf86 100644 --- a/lib/matplotlib/axes/_lines.py +++ b/lib/matplotlib/axes/_lines.py @@ -8,7 +8,6 @@ from matplotlib import patches as mpatches from matplotlib import collections as mcoll from matplotlib.cbook import iterable -from ._base import _AxesBase class LinesAndSpans(object):