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

Skip to content

Commit 737ee9f

Browse files
QuLogicdstansby
authored andcommitted
Add Axes method for drawing infinite lines.
Clean up axline Add axline image test Fix test image Add what's new Add note about log axes Error if trying to draw line on non-linear axes Fix scale checking Fix docstring interpolation Chnage to using xy1, xy2
1 parent 4432fda commit 737ee9f

File tree

4 files changed

+86
-0
lines changed

4 files changed

+86
-0
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
New `axline` method
2+
-------------------
3+
4+
A new `axline` method has been added to draw infinitely long lines that pass
5+
through two points.

lib/matplotlib/axes/_axes.py

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -834,6 +834,75 @@ def axvline(self, x=0, ymin=0, ymax=1, **kwargs):
834834
self.autoscale_view(scalex=scalex, scaley=False)
835835
return l
836836

837+
@docstring.dedent_interpd
838+
def axline(self, xy1, xy2, **kwargs):
839+
"""
840+
Add an infinitely long straight line that passes through two points.
841+
842+
Parameters
843+
----------
844+
xy1, xy2 : length 2 iterables
845+
Points for the line to pass through.
846+
847+
Returns
848+
-------
849+
:class:`~matplotlib.lines.Line2D`
850+
851+
Other Parameters
852+
----------------
853+
Valid kwargs are :class:`~matplotlib.lines.Line2D` properties,
854+
with the exception of 'transform':
855+
856+
%(_Line2D_docstr)s
857+
858+
Examples
859+
--------
860+
* Draw a thick red line passing through (0, 0) with a gradient of 1::
861+
862+
>>> axline((0, 0), (1, 1), linewidth=4, color='r')
863+
864+
865+
See Also
866+
--------
867+
axhline : for horizontal lines
868+
axvline : for vertical lines
869+
870+
Notes
871+
-----
872+
Currently this method does not work properly with non-linear axes.
873+
"""
874+
if not self.get_xscale() == self.get_yscale() == 'linear':
875+
raise NotImplementedError('axline() is only supported on '
876+
'linearly scaled axes')
877+
878+
if "transform" in kwargs:
879+
raise TypeError("'transform' is not allowed as a kwarg; "
880+
"axline generates its own transform.")
881+
882+
x1, y1 = xy1
883+
x2, y2 = xy2
884+
# If x values the same, we have a vertical line
885+
if x1 == x2:
886+
line = self.axvline(x1, **kwargs)
887+
return line
888+
889+
slope = (y2 - y1) / (x2 - x1)
890+
intercept = y1 - (slope * x1)
891+
892+
xtrans = mtransforms.BboxTransformTo(self.viewLim)
893+
viewLimT = mtransforms.TransformedBbox(
894+
self.viewLim,
895+
mtransforms.Affine2D().rotate_deg(90).scale(-1, 1))
896+
ytrans = (mtransforms.BboxTransformTo(viewLimT) +
897+
mtransforms.Affine2D().scale(slope).translate(0, intercept))
898+
trans = mtransforms.blended_transform_factory(xtrans, ytrans)
899+
900+
line = mlines.Line2D([0, 1], [0, 1],
901+
transform=trans + self.transData,
902+
**kwargs)
903+
self.add_line(line)
904+
return line
905+
837906
@docstring.dedent_interpd
838907
def axhspan(self, ymin, ymax, xmin=0, xmax=1, **kwargs):
839908
"""
Loading

lib/matplotlib/tests/test_axes.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3539,6 +3539,18 @@ def test_eb_line_zorder():
35393539
ax.set_title("errorbar zorder test")
35403540

35413541

3542+
@image_comparison(baseline_images=['axline'], extensions=['png'],
3543+
style='mpl20')
3544+
def test_axline():
3545+
fig, ax = plt.subplots()
3546+
ax.set_xlim(-1, 1)
3547+
ax.set_ylim(-1, 1)
3548+
ax.axline((0, 0), (1, 1))
3549+
ax.axline((0, 0), (1, 0), color='C1')
3550+
ax.axline((0, 0.5), (1, 0.5), color='C2')
3551+
ax.axline((0.5, 0), (0.5, 1), color='C3')
3552+
3553+
35423554
@image_comparison(
35433555
baseline_images=['vlines_basic', 'vlines_with_nan', 'vlines_masked'],
35443556
extensions=['png']

0 commit comments

Comments
 (0)