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

Skip to content

Commit a7291a8

Browse files
committed
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.
1 parent 037f25c commit a7291a8

File tree

2 files changed

+84
-70
lines changed

2 files changed

+84
-70
lines changed

lib/matplotlib/axes/_axes.py

Lines changed: 4 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@
3434
from matplotlib.container import BarContainer, ErrorbarContainer, StemContainer
3535
from matplotlib.axes._base import _AxesBase
3636

37+
from . import _lines
38+
3739
iterable = cbook.iterable
3840
is_string_like = cbook.is_string_like
3941
is_sequence_of_strings = cbook.is_sequence_of_strings
@@ -591,77 +593,9 @@ def annotate(self, *args, **kwargs):
591593

592594
#### Lines and spans
593595

594-
@docstring.dedent_interpd
595596
def axhline(self, y=0, xmin=0, xmax=1, **kwargs):
596-
"""
597-
Add a horizontal line across the axis.
598-
599-
Parameters
600-
----------
601-
y : scalar, optional, default: 0
602-
y position in data coordinates of the horizontal line.
603-
604-
xmin : scalar, optional, default: 0
605-
Should be between 0 and 1, 0 being the far left of the plot, 1 the
606-
far right of the plot.
607-
608-
xmax : scalar, optional, default: 1
609-
Should be between 0 and 1, 0 being the far left of the plot, 1 the
610-
far right of the plot.
611-
612-
Returns
613-
-------
614-
`~matplotlib.lines.Line2D`
615-
616-
Notes
617-
-----
618-
kwargs are the same as kwargs to plot, and can be
619-
used to control the line properties. e.g.,
620-
621-
Examples
622-
--------
623-
624-
* draw a thick red hline at 'y' = 0 that spans the xrange::
625-
626-
>>> axhline(linewidth=4, color='r')
627-
628-
* draw a default hline at 'y' = 1 that spans the xrange::
629-
630-
>>> axhline(y=1)
631-
632-
* draw a default hline at 'y' = .5 that spans the the middle half of
633-
the xrange::
634-
635-
>>> axhline(y=.5, xmin=0.25, xmax=0.75)
636-
637-
Valid kwargs are :class:`~matplotlib.lines.Line2D` properties,
638-
with the exception of 'transform':
639-
640-
%(Line2D)s
641-
642-
See also
643-
--------
644-
`axhspan` for example plot and source code
645-
"""
646-
647-
if "transform" in kwargs:
648-
raise ValueError(
649-
"'transform' is not allowed as a kwarg;"
650-
+ "axhline generates its own transform.")
651-
ymin, ymax = self.get_ybound()
652-
653-
# We need to strip away the units for comparison with
654-
# non-unitized bounds
655-
self._process_unit_info(ydata=y, kwargs=kwargs)
656-
yy = self.convert_yunits(y)
657-
scaley = (yy < ymin) or (yy > ymax)
658-
659-
trans = mtransforms.blended_transform_factory(
660-
self.transAxes, self.transData)
661-
l = mlines.Line2D([xmin, xmax], [y, y], transform=trans, **kwargs)
662-
self.add_line(l)
663-
self.autoscale_view(scalex=False, scaley=scaley)
664-
return l
597+
return _lines.axhline(self, y=0, xmin=0, xmax=1, **kwargs)
598+
axhline.__doc__ = _lines.axhline.__doc__
665599

666600
@docstring.dedent_interpd
667601
def axvline(self, x=0, ymin=0, ymax=1, **kwargs):

lib/matplotlib/axes/_lines.py

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
"""
2+
Lines and spans
3+
"""
4+
5+
from matplotlib import docstring
6+
from matplotlib import transforms as mtransforms
7+
from matplotlib import line as mlines
8+
9+
10+
@docstring.dedent_interpd
11+
def axhline(ax, y=0, xmin=0, xmax=1, **kwargs):
12+
"""
13+
Add a horizontal line across the axis.
14+
15+
Parameters
16+
----------
17+
y : scalar, optional, default: 0
18+
y position in data coordinates of the horizontal line.
19+
20+
xmin : scalar, optional, default: 0
21+
Should be between 0 and 1, 0 being the far left of the plot, 1 the
22+
far right of the plot.
23+
24+
xmax : scalar, optional, default: 1
25+
Should be between 0 and 1, 0 being the far left of the plot, 1 the
26+
far right of the plot.
27+
28+
Returns
29+
-------
30+
`~matplotlib.lines.Line2D`
31+
32+
Notes
33+
-----
34+
kwargs are the same as kwargs to plot, and can be
35+
used to control the line properties. e.g.,
36+
37+
Examples
38+
--------
39+
40+
* draw a thick red hline at 'y' = 0 that spans the xrange::
41+
42+
>>> axhline(linewidth=4, color='r')
43+
44+
* draw a default hline at 'y' = 1 that spans the xrange::
45+
46+
>>> axhline(y=1)
47+
48+
* draw a default hline at 'y' = .5 that spans the the middle half of
49+
the xrange::
50+
51+
>>> axhline(y=.5, xmin=0.25, xmax=0.75)
52+
53+
Valid kwargs are :class:`~matplotlib.lines.Line2D` properties,
54+
with the exception of 'transform':
55+
56+
%(Line2D)s
57+
58+
See also
59+
--------
60+
`axhspan` for example plot and source code
61+
"""
62+
63+
if "transform" in kwargs:
64+
raise ValueError(
65+
"'transform' is not allowed as a kwarg;"
66+
+ "axhline generates its own transform.")
67+
ymin, ymax = ax.get_ybound()
68+
69+
# We need to strip away the units for comparison with
70+
# non-unitized bounds
71+
ax._process_unit_info(ydata=y, kwargs=kwargs)
72+
yy = ax.convert_yunits(y)
73+
scaley = (yy < ymin) or (yy > ymax)
74+
75+
trans = mtransforms.blended_transform_factory(
76+
ax.transAxes, ax.transData)
77+
l = mlines.Line2D([xmin, xmax], [y, y], transform=trans, **kwargs)
78+
ax.add_line(l)
79+
ax.autoscale_view(scalex=False, scaley=scaley)
80+
return l

0 commit comments

Comments
 (0)