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

Skip to content

Commit 42ab463

Browse files
committed
Tweak cursor coordinates display format.
1 parent f50c05e commit 42ab463

File tree

2 files changed

+21
-26
lines changed

2 files changed

+21
-26
lines changed

lib/matplotlib/axes/_base.py

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3971,27 +3971,24 @@ def format_coord(self, x, y):
39713971
"""Return a format string formatting the *x*, *y* coordinates."""
39723972
twins = self._twinned_axes.get_siblings(self)
39733973
if len(twins) == 1:
3974-
return "x={} y={}".format(
3974+
return "(x, y) = ({}, {})".format(
39753975
"???" if x is None else self.format_xdata(x),
39763976
"???" if y is None else self.format_ydata(y))
3977-
x_twins = []
3978-
y_twins = []
3977+
screen_xy = self.transData.transform((x, y))
3978+
xy_strs = []
39793979
# Retrieve twins in the order of self.figure.axes to sort tied zorders (which is
39803980
# the common case) by the order in which they are added to the figure.
3981-
for ax in self.figure.axes:
3982-
if ax in twins:
3983-
if ax is self or ax._sharex is self or self._sharex is ax:
3984-
x_twins.append(ax)
3985-
if ax is self or ax._sharey is self or self._sharey is ax:
3986-
y_twins.append(ax)
3987-
screen_xy = self.transData.transform((x, y))
3988-
return "x={} y={}".format(
3989-
" | ".join(
3990-
ax.format_xdata(ax.transData.inverted().transform(screen_xy)[0])
3991-
for ax in sorted(y_twins, key=attrgetter("zorder"))),
3992-
" | ".join(
3993-
ax.format_xdata(ax.transData.inverted().transform(screen_xy)[1])
3994-
for ax in sorted(x_twins, key=attrgetter("zorder"))))
3981+
for ax in sorted(
3982+
[ax for ax in self.figure.axes if (
3983+
ax in twins and (
3984+
ax is self
3985+
or ax._sharex is self or self._sharex is ax
3986+
or ax._sharey is self or self._sharey is ax))],
3987+
key=attrgetter("zorder")):
3988+
data_x, data_y = ax.transData.inverted().transform(screen_xy)
3989+
xy_strs.append(
3990+
"({}, {})".format(ax.format_xdata(data_x), ax.format_ydata(data_y)))
3991+
return "(x, y) = {}".format(" | ".join(xy_strs))
39953992

39963993
def minorticks_on(self):
39973994
"""

lib/matplotlib/tests/test_backend_bases.py

Lines changed: 7 additions & 9 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,21 +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)"
131128

132129

133130
def test_location_event_position_twin():
134131
fig, ax = plt.subplots()
135132
ax.set(xlim=(0, 10), ylim=(0, 20))
136-
assert ax.format_coord(5., 5.) == "x=5.00 y=5.00"
133+
assert ax.format_coord(5., 5.) == "(x, y) = (5.00, 5.00)"
137134
ax.twinx().set(ylim=(0, 40))
138-
assert ax.format_coord(5., 5.) == "x=5.00 y=5.00 | 10.00"
135+
assert ax.format_coord(5., 5.) == "(x, y) = (5.00, 5.00) | (5.00, 10.0)"
139136
ax.twiny().set(xlim=(0, 5))
140-
assert ax.format_coord(5., 5.) == "x=5.00 | 2.50 y=5.00 | 10.00"
137+
assert (ax.format_coord(5., 5.)
138+
== "(x, y) = (5.00, 5.00) | (5.00, 10.0) | (2.50, 5.00)")
141139

142140

143141
def test_pick():

0 commit comments

Comments
 (0)