Skip to content

Refactoring the axes module (part II) #2213

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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 12 commits into from
Next Next commit
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.
  • Loading branch information
NelleV committed Jul 18, 2013
commit a7291a86e90f15e212f15c7ccb279314c44c2d52
74 changes: 4 additions & 70 deletions lib/matplotlib/axes/_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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):
Expand Down
80 changes: 80 additions & 0 deletions lib/matplotlib/axes/_lines.py
Original file line number Diff line number Diff line change
@@ -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