import
matplotlib.pyplot
as
plt
import
numpy
as
np
from
matplotlib.backend_bases
import
from
matplotlib.widgets
import
class
AnnotatedCursor
(
):
"""
A crosshair cursor like `~matplotlib.widgets.Cursor` with a text showing \
the current coordinates.
For the cursor to remain responsive you must keep a reference to it.
The data of the axis specified as *dataaxis* must be in ascending
order. Otherwise, the `numpy.searchsorted` call might fail and the text
disappears. You can satisfy the requirement by sorting the data you plot.
Usually the data is already sorted (if it was created e.g. using
`numpy.linspace`), but e.g. scatter plots might cause this problem.
The cursor sticks to the plotted line.
Parameters
----------
line : `matplotlib.lines.Line2D`
The plot line from which the data coordinates are displayed.
numberformat : `python format string <https://docs.python.org/3/\
library/string.html#formatstrings>`_, optional, default: "{0:.4g};{1:.4g}"
The displayed text is created by calling *format()* on this string
with the two coordinates.
offset : (float, float) default: (5, 5)
The offset in display (pixel) coordinates of the text position
relative to the cross-hair.
dataaxis : {"x", "y"}, optional, default: "x"
If "x" is specified, the vertical cursor line sticks to the mouse
pointer. The horizontal cursor line sticks to *line*
at that x value. The text shows the data coordinates of *line*
at the pointed x value. If you specify "y", it works in the opposite
manner. But: For the "y" value, where the mouse points to, there might
be multiple matching x values, if the plotted function is not biunique.
Cursor and text coordinate will always refer to only one x value.
So if you use the parameter value "y", ensure that your function is
biunique.
Other Parameters
----------------
textprops : `matplotlib.text` properties as dictionary
Specifies the appearance of the rendered text object.
**cursorargs : `matplotlib.widgets.Cursor` properties
Arguments passed to the internal `~matplotlib.widgets.Cursor` instance.
The `matplotlib.axes.Axes` argument is mandatory! The parameter
*useblit* can be set to *True* in order to achieve faster rendering.
"""
def
__init__
(
self
,
,
numberformat
=
"
{0:.4g}
;
{1:.4g}
"
,
offset
=
(
5
,
5
),
dataaxis
=
'x'
,
textprops
=
None
,
**
cursorargs
):
if
textprops
is
None
:
textprops
=
{}
self
.
=
self
.
numberformat
=
numberformat
self
.
offset
=
(
offset
)
self
.
dataaxis
=
dataaxis
super
()
.
__init__
(
**
cursorargs
)
self
.
set_position
(
self
.
.
get_xdata
()[
0
],
self
.
.
get_ydata
()[
0
])
self
.
text
=
self
.
.
text
(
self
.
.
get_xbound
()[
0
],
self
.
.
get_ybound
()[
0
],
"0, 0"
,
animated
=
bool
(
self
.
useblit
),
visible
=
False
,
**
textprops
)
self
.
lastdrawnplotpoint
=
None
def
onmove
(
self
,
event
):
"""
Overridden draw callback for cursor. Called when moving the mouse.
"""
if
self
.
ignore
(
event
):
self
.
lastdrawnplotpoint
=
None
return
if
not
self
.
canvas
.
widgetlock
.
available
(
self
):
self
.
lastdrawnplotpoint
=
None
return
if
event
.
inaxes
!=
self
.
:
self
.
lastdrawnplotpoint
=
None
self
.
text
.
set_visible
(
False
)
super
()
.
onmove
(
event
)
return
plotpoint
=
None
if
event
.
xdata
is
not
None
and
event
.
ydata
is
not
None
:
plotpoint
=
self
.
set_position
(
event
.
xdata
,
event
.
ydata
)
if
plotpoint
is
not
None
:
event
.
xdata
=
plotpoint
[
0
]
event
.
ydata
=
plotpoint
[
1
]
if
plotpoint
is
not
None
and
plotpoint
==
self
.
lastdrawnplotpoint
:
return
super
()
.
onmove
(
event
)
if
not
self
.
get_active
()
or
not
self
.
visible
:
return
if
plotpoint
is
not
None
:
temp
=
[
event
.
xdata
,
event
.
ydata
]
temp
=
self
.
.
transform
(
temp
)
temp
=
temp
+
self
.
offset
temp
=
self
.
.
inverted
()
.
transform
(
temp
)
self
.
text
.
set_position
(
temp
)
self
.
text
.
set_text
(
self
.
numberformat
.
format
(
*
plotpoint
))
self
.
text
.
set_visible
(
self
.
visible
)
self
.
needclear
=
True
self
.
lastdrawnplotpoint
=
plotpoint
else
:
self
.
text
.
set_visible
(
False
)
if
self
.
useblit
:
self
.
.
draw_artist
(
self
.
text
)
self
.
canvas
.
blit
(
self
.
.
bbox
)
else
:
self
.
canvas
.
draw_idle
()
def
set_position
(
self
,
xpos
,
ypos
):
"""
Finds the coordinates, which have to be shown in text.
The behaviour depends on the *dataaxis* attribute. Function looks
up the matching plot coordinate for the given mouse position.
Parameters
----------
xpos : float
The current x position of the cursor in data coordinates.
Important if *dataaxis* is set to 'x'.
ypos : float
The current y position of the cursor in data coordinates.
Important if *dataaxis* is set to 'y'.
Returns
-------
ret : {2D array-like, None}
The coordinates which should be displayed.
*None* is the fallback value.
"""
xdata
=
self
.
.
get_xdata
()
ydata
=
self
.
.
get_ydata
()
if
self
.
dataaxis
==
'x'
:
pos
=
xpos
data
=
xdata
lim
=
self
.
.
get_xlim
()
elif
self
.
dataaxis
==
'y'
:
pos
=
ypos
data
=
ydata
lim
=
self
.
.
get_ylim
()
else
:
raise
ValueError
(
f
"The data axis specifier
{
self
.
dataaxis
}
should "
f
"be 'x' or 'y'"
)
if
pos
is
not
None
and
lim
[
0
]
<=
pos
<=
lim
[
-
1
]:
index
=
(
data
,
pos
)
if
index
<
0
or
index
>=
len
(
data
):
return
None
return
(
xdata
[
index
],
ydata
[
index
])
return
None
def
clear
(
self
,
event
):
"""
Overridden clear callback for cursor, called before drawing the figure.
"""
super
()
.
clear
(
event
)
if
self
.
ignore
(
event
):
return
self
.
text
.
set_visible
(
False
)
def
_update
(
self
):
"""
Overridden method for either blitting or drawing the widget canvas.
Passes call to base class if blitting is activated, only.
In other cases, one draw_idle call is enough, which is placed
explicitly in this class (see *onmove()*).
In that case, `~matplotlib.widgets.Cursor` is not supposed to draw
something using this method.
"""
if
self
.
useblit
:
super
()
.
_update
()
,
=
(
figsize
=
(
8
,
6
))
(
"Cursor Tracking x Position"
)
=
(
-
5
,
5
,
1000
)
=
**
2
,
=
(
,
)
(
-
5
,
5
)
(
0
,
25
)
cursor
=
(
=
,
numberformat
=
"
{0:.2f}
\n
{1:.2f}
"
,
dataaxis
=
'x'
,
offset
=
[
10
,
10
],
textprops
=
{
'color'
:
'blue'
,
'fontweight'
:
'bold'
},
=
,
useblit
=
True
,
color
=
'red'
,
linewidth
=
2
)
=
(
"motion_notify_event"
,
,
*
((
-
2
,
10
))
)
.
_process
()
()
Download zipped: annotated_cursor.zip