@@ -919,6 +919,79 @@ def axvline(self, x=0, ymin=0, ymax=1, **kwargs):
919
919
self ._request_autoscale_view (scalex = scalex , scaley = False )
920
920
return l
921
921
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
+
922
995
@docstring .dedent_interpd
923
996
def axhspan (self , ymin , ymax , xmin = 0 , xmax = 1 , ** kwargs ):
924
997
"""
0 commit comments