-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Add axes method for drawing infinite lines. #15330
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -87,6 +87,7 @@ Spans | |
Axes.axhspan | ||
Axes.axvline | ||
Axes.axvspan | ||
Axes.axline | ||
|
||
Spectral | ||
-------- | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
New `~.axes.Axes.axline` method | ||
------------------------------- | ||
|
||
A new `~.axes.Axes.axline` method has been added to draw infinitely long lines | ||
that pass through two points. |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -816,6 +816,7 @@ def axhline(self, y=0, xmin=0, xmax=1, **kwargs): | |
-------- | ||
hlines : Add horizontal lines in data coordinates. | ||
axhspan : Add a horizontal span (rectangle) across the axis. | ||
axline : Add a line with an arbitrary slope. | ||
|
||
Examples | ||
-------- | ||
|
@@ -899,6 +900,7 @@ def axvline(self, x=0, ymin=0, ymax=1, **kwargs): | |
-------- | ||
vlines : Add vertical lines in data coordinates. | ||
axvspan : Add a vertical span (rectangle) across the axis. | ||
axline : Add a line with an abritrary slope. | ||
""" | ||
|
||
if "transform" in kwargs: | ||
|
@@ -919,6 +921,63 @@ def axvline(self, x=0, ymin=0, ymax=1, **kwargs): | |
self._request_autoscale_view(scalex=scalex, scaley=False) | ||
return l | ||
|
||
@docstring.dedent_interpd | ||
def axline(self, xy1, xy2, **kwargs): | ||
""" | ||
Add an infinitely long straight line that passes through two points. | ||
|
||
This draws a straight line "on the screen", regardless of the x and y | ||
scales, and is thus also suitable for drawing exponential decays in | ||
semilog plots, power laws in loglog plots, etc. | ||
|
||
Parameters | ||
---------- | ||
xy1, xy2 : (float, float) | ||
Points for the line to pass through. | ||
|
||
Returns | ||
------- | ||
:class:`~matplotlib.lines.Line2D` | ||
|
||
Other Parameters | ||
---------------- | ||
**kwargs | ||
Valid kwargs are :class:`~matplotlib.lines.Line2D` properties, | ||
with the exception of 'transform': | ||
|
||
%(_Line2D_docstr)s | ||
|
||
Examples | ||
-------- | ||
Draw a thick red line passing through (0, 0) and (1, 1):: | ||
|
||
>>> axline((0, 0), (1, 1), linewidth=4, color='r') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add an example in log space... There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. actually this example works fine (but demonstrates vlines/hlines which behave stupidly differently from axvline/axhline) -- the example that needs to be modified is https://matplotlib.org/gallery/subplots_axes_and_figures/axhspan_demo.html. |
||
|
||
See Also | ||
-------- | ||
axhline : for horizontal lines | ||
axvline : for vertical lines | ||
""" | ||
|
||
if "transform" in kwargs: | ||
raise TypeError("'transform' is not allowed as a kwarg; " | ||
"axline generates its own transform") | ||
x1, y1 = xy1 | ||
x2, y2 = xy2 | ||
line = mlines._AxLine([x1, x2], [y1, y2], **kwargs) | ||
# Like add_line, but correctly handling data limits. | ||
self._set_artist_props(line) | ||
if line.get_clip_path() is None: | ||
line.set_clip_path(self.patch) | ||
if not line.get_label(): | ||
line.set_label(f"_line{len(self.lines)}") | ||
self.lines.append(line) | ||
line._remove_method = self.lines.remove | ||
self.update_datalim([xy1, xy2]) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It might be nice to provide a way to exclude this There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To elaborate on this comment: I've come across two use-cases where this scaling behavior is undesirable:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I agree that it should be possible to skip the autoscaling. The two points being specified only need to be somewhere along the desired infinite line, and it's perfectly reasonable that they might not be within the domain one actually wants to view. I'm actually wondering whether it would make more sense to leave out the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's move this discussion to #16264 (which I replied to already). |
||
|
||
self._request_autoscale_view() | ||
return line | ||
|
||
@docstring.dedent_interpd | ||
def axhspan(self, ymin, ymax, xmin=0, xmax=1, **kwargs): | ||
""" | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -194,6 +194,7 @@ def boilerplate_gen(): | |
'axhline', | ||
'axhspan', | ||
'axis', | ||
'axline', | ||
'axvline', | ||
'axvspan', | ||
'bar', | ||
|
Uh oh!
There was an error while loading. Please reload this page.