-
Notifications
You must be signed in to change notification settings - Fork 21
/
_matplotlib_axes.py
228 lines (184 loc) · 8.27 KB
/
_matplotlib_axes.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
"""
This joins our Axes model to matplotlib.axes.Axes. It is used by
bluesky_widgets.qt.figures and bluesky_widgets.jupyter.figures.
"""
import logging
from .models.plot_specs import Axes, Line, Image
class MatplotlibAxes:
"""
Respond to changes in Axes by manipulating matplotlib.axes.Axes.
Note that while most view classes accept model as their only __init__
parameter, this view class expects matplotlib.axes.Axes as well. If we
follow the pattern used elsewhere in bluesky-widgets, we would want to
receive only the model and to create matplotlib.axes.Axes internally in
this class.
The reason we break the pattern is pragmatic: matplotlib's
plt.subplots(...) function is the easiest way to create a Figure and Axes
with a nice layout, and it creates both Figure and Axes. So, this class
receives pre-made Axes from the outside, ultimately via plt.subplots(...).
"""
def __init__(self, model: Axes, axes, *args, **kwargs):
super().__init__(*args, **kwargs)
self.model = model
self.axes = axes
self.type_map = {
Line: self._construct_line,
Image: self._construct_image,
}
# If we specify data limits and axes aspect and position, we have
# overdetermined the system. When these are incompatible, we want
# matplotlib to expand the data limts along one dimension rather than
# disorting the boundaries of the axes (for example, creating a tall,
# shinny axes box).
self.axes.set_adjustable("datalim")
axes.set_title(model.title)
axes.set_xlabel(model.x_label)
axes.set_ylabel(model.y_label)
aspect = model.aspect or "auto"
axes.set_aspect(aspect)
if model.x_limits is not None:
axes.set_xlim(model.x_limits)
if model.y_limits is not None:
axes.set_ylim(model.y_limits)
# Use matplotlib's user-configurable ID so that we can look up the
# Axes from the axes if we need to.
axes.set_gid(model.uuid)
# Keep a reference to all types of artist here.
self._artists = {}
for artist in model.artists:
self._add_artist(artist)
self.connect(model.artists.events.added, self._on_artist_spec_added)
self.connect(model.artists.events.removed, self._on_artist_spec_removed)
self.connect(model.events.title, self._on_title_changed)
self.connect(model.events.x_label, self._on_x_label_changed)
self.connect(model.events.y_label, self._on_y_label_changed)
self.connect(model.events.aspect, self._on_aspect_changed)
self.connect(model.events.x_limits, self._on_x_limits_changed)
self.connect(model.events.y_limits, self._on_y_limits_changed)
def connect(self, emitter, callback):
"""
Add a callback to an emitter.
This is exposed as a separate method so that The Qt view can override
it this with a threadsafe connect.
"""
emitter.connect(callback)
def draw_idle(self):
"""
Re-draw the figure when the UI is ready.
This is exposed as a separate method so that it can be overriden with a
more aggressive draw() for debugging in contexts where thread-safety
is not a concern.
"""
self.axes.figure.canvas.draw_idle()
def _on_title_changed(self, event):
self.axes.set_title(event.value)
self._update_and_draw()
def _on_x_label_changed(self, event):
self.axes.set_xlabel(event.value)
self._update_and_draw()
def _on_y_label_changed(self, event):
self.axes.set_ylabel(event.value)
self._update_and_draw()
def _on_aspect_changed(self, event):
aspect = event.value or "auto"
self.axes.set_aspect(aspect)
self._update_and_draw()
def _on_x_limits_changed(self, event):
self.axes.set_xlim(event.value)
self._update_and_draw()
def _on_y_limits_changed(self, event):
self.axes.set_ylim(event.value)
self._update_and_draw()
def _on_artist_spec_added(self, event):
artist_spec = event.item
self._add_artist(artist_spec)
def _add_artist(self, artist_spec):
"""
Add an artist.
"""
# Initialize artist with currently-available data.
constructor = self.type_map[type(artist_spec)]
artist, update = constructor(
**artist_spec.update(),
label=artist_spec.label,
style=artist_spec.style,
)
def handle_new_data(event):
update(**artist_spec.update())
if artist_spec.live:
self.connect(artist_spec.events.new_data, handle_new_data)
self.connect(
artist_spec.events.completed,
lambda event: artist_spec.events.new_data.disconnect(handle_new_data),
)
# Track it as a generic artist cache and in a type-specific cache.
self._artists[artist_spec.uuid] = artist
# Use matplotlib's user-configurable ID so that we can look up the
# ArtistSpec from the artist artist if we need to.
artist.set_gid(artist_spec.uuid)
# Listen for changes to label and style.
self.connect(artist_spec.events.label, self._on_label_changed)
self.connect(artist_spec.events.style_updated, self._on_style_updated)
self._update_and_draw()
def _on_label_changed(self, event):
artist_spec = event.artist_spec
artist = self._artists[artist_spec.uuid]
artist.set(label=event.value)
self._update_and_draw()
def _on_style_updated(self, event):
artist_spec = event.artist_spec
artist = self._artists[artist_spec.uuid]
artist.set(**event.update)
self._update_and_draw()
def _on_artist_spec_removed(self, event):
artist_spec = event.item
# Remove the artist from our caches.
artist = self._artists.pop(artist_spec.uuid)
# Remove colorbar if it exists
if hasattr(artist, "_bsw_colorbar"):
cb = getattr(artist, "_bsw_colorbar")
cb.remove()
# Remove it from the canvas.
artist.remove()
self._update_and_draw()
def _update_and_draw(self):
"Update the legend and redraw the canvas."
self.axes.legend(loc=1) # Update the legend.
self.axes.relim()
self.axes.autoscale_view()
self.draw_idle() # Ask matplotlib to redraw the figure.
# These wrapper factory functions build various matplotlib Artist types (e.g.
# Line2D, AxesImage) and translate between their creation and update APIs
# and ours. In general matplotlib Artists are not consistent between their
# creation and update signatures, so we need this amount of wrapping.
def _construct_line(self, *, x, y, label, style):
(artist,) = self.axes.plot(x, y, label=label, **style)
self.axes.relim() # Recompute data limits.
self.axes.autoscale_view() # Rescale the view using those new limits.
self.draw_idle()
def update(*, x, y):
artist.set_data(x, y)
self.axes.relim() # Recompute data limits.
self.axes.autoscale_view() # Rescale the view using those new limits.
self.draw_idle()
return artist, update
def _construct_image(self, *, array, label, style):
artist = self.axes.imshow(array, label=label)
if style.get("show_colorbar", False):
cb = self.axes.figure.colorbar(artist)
# Keep the reference to the colorbar so that it could be removed with the artist
setattr(artist, "_bsw_colorbar", cb) # bsw - bluesky-widgets
self.axes.relim() # Recompute data limits.
self.axes.autoscale_view() # Rescale the view using those new limits.
self.draw_idle()
def update(*, array):
artist.set_data(array)
self.axes.relim() # Recompute data limits.
self.axes.autoscale_view() # Rescale the view using those new limits.
self.draw_idle()
return artist, update
def _quiet_mpl_noisy_logger():
"Do not filter or silence it, but avoid defaulting to the logger of last resort."
logger = logging.getLogger("matplotlib.legend")
logger.addHandler(logging.NullHandler())
_quiet_mpl_noisy_logger()