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

Skip to content

Commit 9516863

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 9221a55 commit 9516863

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
@@ -908,6 +908,75 @@ def axvline(self, x=0, ymin=0, ymax=1, **kwargs):
908908
self.autoscale_view(scalex=scalex, scaley=False)
909909
return l
910910

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

lib/matplotlib/tests/test_axes.py

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

35063506

3507+
@image_comparison(baseline_images=['axline'], extensions=['png'],
3508+
style='mpl20')
3509+
def test_axline():
3510+
fig, ax = plt.subplots()
3511+
ax.set_xlim(-1, 1)
3512+
ax.set_ylim(-1, 1)
3513+
ax.axline((0, 0), (1, 1))
3514+
ax.axline((0, 0), (1, 0), color='C1')
3515+
ax.axline((0, 0.5), (1, 0.5), color='C2')
3516+
ax.axline((0.5, 0), (0.5, 1), color='C3')
3517+
3518+
35073519
@image_comparison(
35083520
baseline_images=['vlines_basic', 'vlines_with_nan', 'vlines_masked'],
35093521
extensions=['png']

0 commit comments

Comments
 (0)