@@ -816,7 +816,7 @@ def axhline(self, y=0, xmin=0, xmax=1, **kwargs):
816816 --------
817817 hlines : Add horizontal lines in data coordinates.
818818 axhspan : Add a horizontal span (rectangle) across the axis.
819- axhline : Add a line with an arbitrary slope.
819+ axline : Add a line with an arbitrary slope.
820820
821821 Examples
822822 --------
@@ -926,6 +926,10 @@ def axline(self, xy1, xy2, **kwargs):
926926 """
927927 Add an infinitely long straight line that passes through two points.
928928
929+ This draws a straight line "on the screen", regardless of the x and y
930+ scales, and is thus also suitable for drawing exponential decays in
931+ semilog plots, power laws in loglog plots, etc.
932+
929933 Parameters
930934 ----------
931935 xy1, xy2 : (float, float)
@@ -937,61 +941,41 @@ def axline(self, xy1, xy2, **kwargs):
937941
938942 Other Parameters
939943 ----------------
940- Valid kwargs are :class:`~matplotlib.lines.Line2D` properties,
941- with the exception of 'transform':
944+ **kwargs
945+ Valid kwargs are :class:`~matplotlib.lines.Line2D` properties,
946+ with the exception of 'transform':
942947
943- %(_Line2D_docstr)s
948+ %(_Line2D_docstr)s
944949
945950 Examples
946951 --------
947952 Draw a thick red line passing through (0, 0) and (1, 1)::
948953
949954 >>> axline((0, 0), (1, 1), linewidth=4, color='r')
950955
951-
952956 See Also
953957 --------
954958 axhline : for horizontal lines
955959 axvline : for vertical lines
956-
957- Notes
958- -----
959- Currently this method does not work properly with non-linear axes.
960960 """
961- if not self .get_xscale () == self .get_yscale () == 'linear' :
962- raise NotImplementedError ('axline() is only supported on '
963- 'linearly scaled axes' )
964961
965962 if "transform" in kwargs :
966963 raise TypeError ("'transform' is not allowed as a kwarg; "
967- "axline generates its own transform." )
968-
964+ "axline generates its own transform" )
969965 x1 , y1 = xy1
970966 x2 , y2 = xy2
971- # If x values the same, we have a vertical line
972- if np .allclose (x1 , x2 ):
973- if np .allclose (y1 , y2 ):
974- raise ValueError (
975- 'Cannot draw a line through two identical points '
976- f'(got x1={ x1 } , x2={ x2 } , y1={ y1 } , y2={ y2 } ).' )
977- line = self .axvline (x1 , ** kwargs )
978- return line
979-
980- slope = (y2 - y1 ) / (x2 - x1 )
981- intercept = y1 - (slope * x1 )
982-
983- xtrans = mtransforms .BboxTransformTo (self .viewLim )
984- viewLimT = mtransforms .TransformedBbox (
985- self .viewLim ,
986- mtransforms .Affine2D ().rotate_deg (90 ).scale (- 1 , 1 ))
987- ytrans = (mtransforms .BboxTransformTo (viewLimT ) +
988- mtransforms .Affine2D ().scale (slope ).translate (0 , intercept ))
989- trans = mtransforms .blended_transform_factory (xtrans , ytrans )
990-
991- line = mlines .Line2D ([0 , 1 ], [0 , 1 ],
992- transform = trans + self .transData ,
993- ** kwargs )
994- self .add_line (line )
967+ line = mlines ._AxLine ([x1 , x2 ], [y1 , y2 ], ** kwargs )
968+ # Like add_line, but correctly handling data limits.
969+ self ._set_artist_props (line )
970+ if line .get_clip_path () is None :
971+ line .set_clip_path (self .patch )
972+ if not line .get_label ():
973+ line .set_label (f"_line{ len (self .lines )} " )
974+ self .lines .append (line )
975+ line ._remove_method = self .lines .remove
976+ self .update_datalim ([xy1 , xy2 ])
977+
978+ self ._request_autoscale_view ()
995979 return line
996980
997981 @docstring .dedent_interpd
0 commit comments