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

Skip to content

Commit a6198af

Browse files
QuLogicanntzer
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 Fix docs and closeness checking Raise error if points are the same Swap axline test to image comparison
1 parent 418984d commit a6198af

File tree

3 files changed

+93
-0
lines changed

3 files changed

+93
-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: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -919,6 +919,79 @@ def axvline(self, x=0, ymin=0, ymax=1, **kwargs):
919919
self._request_autoscale_view(scalex=scalex, scaley=False)
920920
return l
921921

922+
@docstring.dedent_interpd
923+
def axline(self, xy1, xy2, **kwargs):
924+
"""
925+
Add an infinitely long straight line that passes through two points.
926+
927+
Parameters
928+
----------
929+
xy1, xy2 : (float, float)
930+
Points for the line to pass through.
931+
932+
Returns
933+
-------
934+
:class:`~matplotlib.lines.Line2D`
935+
936+
Other Parameters
937+
----------------
938+
Valid kwargs are :class:`~matplotlib.lines.Line2D` properties,
939+
with the exception of 'transform':
940+
941+
%(_Line2D_docstr)s
942+
943+
Examples
944+
--------
945+
* Draw a thick red line passing through (0, 0) with a gradient of 1::
946+
947+
>>> axline((0, 0), (1, 1), linewidth=4, color='r')
948+
949+
950+
See Also
951+
--------
952+
axhline : for horizontal lines
953+
axvline : for vertical lines
954+
955+
Notes
956+
-----
957+
Currently this method does not work properly with non-linear axes.
958+
"""
959+
if not self.get_xscale() == self.get_yscale() == 'linear':
960+
raise NotImplementedError('axline() is only supported on '
961+
'linearly scaled axes')
962+
963+
if "transform" in kwargs:
964+
raise TypeError("'transform' is not allowed as a kwarg; "
965+
"axline generates its own transform.")
966+
967+
x1, y1 = xy1
968+
x2, y2 = xy2
969+
# If x values the same, we have a vertical line
970+
if np.allclose(x1, x2):
971+
if np.allclose(y1, y2):
972+
raise ValueError(
973+
'Cannot draw a line through two identical points '
974+
f'(got x1={x1}, x2={x2}, y1={y1}, y2={y2}).')
975+
line = self.axvline(x1, **kwargs)
976+
return line
977+
978+
slope = (y2 - y1) / (x2 - x1)
979+
intercept = y1 - (slope * x1)
980+
981+
xtrans = mtransforms.BboxTransformTo(self.viewLim)
982+
viewLimT = mtransforms.TransformedBbox(
983+
self.viewLim,
984+
mtransforms.Affine2D().rotate_deg(90).scale(-1, 1))
985+
ytrans = (mtransforms.BboxTransformTo(viewLimT) +
986+
mtransforms.Affine2D().scale(slope).translate(0, intercept))
987+
trans = mtransforms.blended_transform_factory(xtrans, ytrans)
988+
989+
line = mlines.Line2D([0, 1], [0, 1],
990+
transform=trans + self.transData,
991+
**kwargs)
992+
self.add_line(line)
993+
return line
994+
922995
@docstring.dedent_interpd
923996
def axhspan(self, ymin, ymax, xmin=0, xmax=1, **kwargs):
924997
"""

lib/matplotlib/tests/test_axes.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3612,6 +3612,21 @@ def test_eb_line_zorder():
36123612
ax.set_title("errorbar zorder test")
36133613

36143614

3615+
@check_figures_equal()
3616+
def test_axline(fig_test, fig_ref):
3617+
ax = fig_test.subplots()
3618+
ax.set(xlim=(-1, 1), ylim=(-1, 1))
3619+
ax.axline((0, 0), (1, 1))
3620+
ax.axline((0, 0), (1, 0), color='C1')
3621+
ax.axline((0, 0.5), (1, 0.5), color='C2')
3622+
3623+
ax = fig_ref.subplots()
3624+
ax.set(xlim=(-1, 1), ylim=(-1, 1))
3625+
ax.plot([-1, 1], [-1, 1])
3626+
ax.axhline(0, color='C1')
3627+
ax.axhline(0.5, color='C2')
3628+
3629+
36153630
@image_comparison(['vlines_basic', 'vlines_with_nan', 'vlines_masked'],
36163631
extensions=['png'])
36173632
def test_vlines():

0 commit comments

Comments
 (0)