Matplotlib Utility and Interaction Functions
Matplotlib Reference Documentation
This group of functions provides event handling, property manipulation, mouse interaction, and debugging tools.
Function overview
| function | Function |
|---|---|
| connect() | Bind an event callback function |
| disconnect() | Unbind an event callback function |
| ginput() | Get coordinates via mouse clicks |
| waitforbuttonpress() | Wait for a Mouse or Key Press |
| findobj() | Find Artists that match a condition |
| get() / setp() | Get/set Artist properties |
| getp() | Get Artist properties (alias of get) |
| get_current_fig_manager() | Get the current Figure manager |
| new_figure_manager() | Create a manager for a Figure |
| set_loglevel() | Set the logging level |
| xkcd() | Switch to the xkcd hand-drawn comic style |
Event handling - connect() / disconnect()
matplotlib.pyplot.connect(s, func)
matplotlib.pyplot.disconnect(cid)
Example
import
matplotlib.
pyplot
as
plt
import
numpy
as
np
fig
,
ax
=
plt.
subplots
(
figsize
=
(
6
,
4
)
)
ax.
plot
(
np.
random
.
randn
(
50
)
.
cumsum
(
)
)
ax.
set_title
(
'Click anywhere on the figure'
)
Click event callback
def
on_click
(
event
)
:
(
f
'Clicked at: x={event.xdata:.2f}, y={event.ydata:.2f}'
)
Bind an event
cid
=
fig.
canvas
.
mpl_connect
(
'button_press_event'
,
on_click
)
plt.
show
(
)
(
"runoob: click events registered"
)
ginput() - select coordinates with the mouse
matplotlib.pyplot.ginput(n=1, timeout=30, show_clicks=True,
mouse_add=MouseButton.LEFT, mouse_pop=MouseButton.RIGHT,
mouse_stop=MouseButton.MIDDLE)
Example
import
matplotlib.
pyplot
as
plt
import
numpy
as
np
fig
,
ax
=
plt.
subplots
(
figsize
=
(
7
,
5
)
)
ax.
plot
(
np.
arange
(
10
)
,
np.
random
.
randn
(
10
)
,
'o-'
)
ax.
set_title
(
'Click 3 points (timeout=15s)'
)
ax.
set_xlim
(
1
,
10
)
ax.
set_ylim
(
3
,
3
)
ax.
grid
(
True
,
alpha
=
0.3
)
(
"Please click 3 points..."
)
points
=
plt.
ginput
(
3
,
timeout
=
15
,
show_clicks
=
True
)
for
i
,
(
x
,
y
)
in
enumerate
(
points
)
:
(
f
"Point {i+1}: ({x:.2f}, {y:.2f})"
)
plt.
show
(
)
waitforbuttonpress() - wait for a key press
matplotlib.pyplot.waitforbuttonpress(timeout=-1)
Return value: True indicates a key press, False indicates a mouse press.
Example
import
matplotlib.
pyplot
as
plt
import
numpy
as
np
fig
,
ax
=
plt.
subplots
(
)
ax.
plot
(
np.
sin
(
np.
linspace
(
0
,
10
,
100
)
)
)
ax.
set_title
(
'Press any key or click mouse'
)
(
"Waiting for button press..."
)
result
=
plt.
waitforbuttonpress
(
timeout
=
10
)
if
result
is
None
:
(
"Timeout!"
)
elif
result:
(
"Key was pressed"
)
else
:
(
"Mouse was clicked"
)
plt.
show
(
)
findobj() - find Artists
matplotlib.pyplot.findobj(o=None, match=None, include_self=False)
Example
import
matplotlib.
pyplot
as
plt
import
numpy
as
np
from
matplotlib.
lines
import
Line2D
fig
,
ax
=
plt.
subplots
(
)
ax.
plot
(
[
1
,
2
,
3
]
,
[
4
,
5
,
6
]
,
label
=
'Line A'
)
ax.
plot
(
[
1
,
2
,
3
]
,
[
1
,
2
,
3
]
,
label
=
'Line B'
)
ax.
set_title
(
'Find Objects Demo'
)
Find all Line2D objects
lines
=
ax.
findobj
(
match
=
Line2D
)
(
f
"Found {len(lines)} line objects"
)
Get specific properties
for
line
in
lines:
(
f
" - label: {line.get_label()}"
)
Use get() to retrieve properties
ax_title
=
ax.
get_title
(
)
(
f
"Axes title: {ax_title}"
)
plt.
show
(
)
Found 2 line objects
- label: Line A
- label: Line B
Axes title: Find Objects Demo
get() / setp() - property manipulation
matplotlib.pyplot.get(obj, *args, **kwargs)
matplotlib.pyplot.setp(obj, *args, **kwargs)
matplotlib.pyplot.getp(obj, *args, **kwargs)
Example
import
matplotlib.
pyplot
as
plt
import
numpy
as
np
fig
,
ax
=
plt.
subplots
(
layout
=
'constrained'
)
line
,
=
ax.
plot
(
[
0
,
1
]
,
[
0
,
1
]
,
label
=
'test'
)
get() retrieves a single property
(
"line color:"
,
plt.
get
(
line
,
'color'
)
)
getp() retrieves multiple properties
props
=
plt.
getp
(
line
)
(
f
"line has {len(props)} properties"
)
setp() sets multiple properties at once
plt.
setp
(
line
,
color
=
'red'
,
linewidth
=
3
,
linestyle
=
'--'
)
ax.
set_title
(
'setp() demo'
)
ax.
legend
(
)
plt.
show
(
)
(
"runoob: properties modified via setp()"
)
xkcd() - hand-drawn comic style
matplotlib.pyplot.xkcd(scale=1, length=100, randomness=2)
Example
import
matplotlib.
pyplot
as
plt
import
numpy
as
np
Enable the xkcd hand-drawn style
with
plt.
xkcd
(
)
:
fig
,
ax
=
plt.
subplots
(
figsize
=
(
8
,
5
)
)
x
=
np.
linspace
(
0
,
10
,
100
)
ax.
plot
(
x
,
np.
sin
(
x
)
,
label
=
'sin(x)'
)
ax.
plot
(
x
,
np.
cos
(
x
)
,
label
=
'cos(x)'
)
ax.
set_title
(
'XKCD-style Plot'
)
ax.
set_xlabel
(
'Time'
)
ax.
set_ylabel
(
'Amplitude'
)
ax.
legend
(
)
ax.
annotate
(
'This is peak!'
,
xy
=
(
np.
pi
/
2
,
1
)
,
xytext
=
(
2
,
1.3
)
,
arrowprops
=
dict
(
arrowstyle
=
'->'
)
)
plt.
show
(
)
(
"runoob: xkcd style displayed"
)
set_loglevel() - logging level
matplotlib.pyplot.set_loglevel(level)
level options: 'debug', 'info', 'warning', 'error', 'critical'.
Example
import
matplotlib.
pyplot
as
plt
import
numpy
as
np
Set the logging level to info (view Matplotlib's internal information)
plt.
set_loglevel
(
'info'
)
fig
,
ax
=
plt.
subplots
(
)
ax.
plot
(
[
1
,
2
,
3
]
,
[
4
,
5
,
6
]
)
ax.
set_title
(
'Debugging with loglevel'
)
plt.
show
(
)