Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit c5b2158

Browse filesBrowse files
committed
Display cursor coordinates for all axes twinned with the current one.
1 parent ae7f769 commit c5b2158
Copy full SHA for c5b2158

File tree

Expand file treeCollapse file tree

2 files changed

+28
-10
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+28
-10
lines changed

‎lib/matplotlib/axes/_base.py

Copy file name to clipboardExpand all lines: lib/matplotlib/axes/_base.py
+14-4Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3976,10 +3976,20 @@ def format_ydata(self, y):
39763976

39773977
def format_coord(self, x, y):
39783978
"""Return a format string formatting the *x*, *y* coordinates."""
3979-
return "x={} y={}".format(
3980-
"???" if x is None else self.format_xdata(x),
3981-
"???" if y is None else self.format_ydata(y),
3982-
)
3979+
twins = self._twinned_axes.get_siblings(self)
3980+
if len(twins) == 1:
3981+
return "(x, y) = ({}, {})".format(
3982+
"???" if x is None else self.format_xdata(x),
3983+
"???" if y is None else self.format_ydata(y))
3984+
screen_xy = self.transData.transform((x, y))
3985+
xy_strs = []
3986+
# Retrieve twins in the order of self.figure.axes to sort tied zorders (which is
3987+
# the common case) by the order in which they are added to the figure.
3988+
for ax in sorted(twins, key=attrgetter("zorder")):
3989+
data_x, data_y = ax.transData.inverted().transform(screen_xy)
3990+
xy_strs.append(
3991+
"({}, {})".format(ax.format_xdata(data_x), ax.format_ydata(data_y)))
3992+
return "(x, y) = {}".format(" | ".join(xy_strs))
39833993

39843994
def minorticks_on(self):
39853995
"""

‎lib/matplotlib/tests/test_backend_bases.py

Copy file name to clipboardExpand all lines: lib/matplotlib/tests/test_backend_bases.py
+14-6Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import re
2-
31
from matplotlib import path, transforms
42
from matplotlib.backend_bases import (
53
FigureCanvasBase, KeyEvent, LocationEvent, MouseButton, MouseEvent,
@@ -123,11 +121,21 @@ def test_location_event_position(x, y):
123121
assert event.y == int(y)
124122
assert isinstance(event.y, int)
125123
if x is not None and y is not None:
126-
assert re.match(
127-
f"x={ax.format_xdata(x)} +y={ax.format_ydata(y)}",
128-
ax.format_coord(x, y))
124+
assert (ax.format_coord(x, y)
125+
== f"(x, y) = ({ax.format_xdata(x)}, {ax.format_ydata(y)})")
129126
ax.fmt_xdata = ax.fmt_ydata = lambda x: "foo"
130-
assert re.match("x=foo +y=foo", ax.format_coord(x, y))
127+
assert ax.format_coord(x, y) == "(x, y) = (foo, foo)"
128+
129+
130+
def test_location_event_position_twin():
131+
fig, ax = plt.subplots()
132+
ax.set(xlim=(0, 10), ylim=(0, 20))
133+
assert ax.format_coord(5., 5.) == "(x, y) = (5.00, 5.00)"
134+
ax.twinx().set(ylim=(0, 40))
135+
assert ax.format_coord(5., 5.) == "(x, y) = (5.00, 5.00) | (5.00, 10.0)"
136+
ax.twiny().set(xlim=(0, 5))
137+
assert (ax.format_coord(5., 5.)
138+
== "(x, y) = (5.00, 5.00) | (5.00, 10.0) | (2.50, 5.00)")
131139

132140

133141
def test_pick():

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.