def __init__(self,fig,init_func=None,event_source=None,interval=10): self._data = deque() self._lag = 0 self._drawn = False self._fig = fig self._interval = interval self._init_func = init_func if event_source is None: event_source = fig.canvas.new_timer(interval=self._interval) Animation.__init__(self,fig,event_source=event_source,blit=True)
def __init__(self, fig, init_func=None, event_source=None, interval=10): self._data = deque() self._lag = 0 self._drawn = False self._fig = fig self._interval = interval self._init_func = init_func if event_source is None: event_source = fig.canvas.new_timer(interval=self._interval) Animation.__init__(self, fig, event_source=event_source, blit=True)
def _step(self, *args): still_going = Animation._step(self, *args) if not still_going: # If self._end_func includes plt.close, returning False raises an exception # So, belows are workaround self.event_source.remove_callback(self._step) self._end_func() return True
def draw_animated(self, alpha: float = None, color_by: Callable[[np.ndarray], float] = None, color_map: str = 'viridis', interval: int = 10, draw_every: int = 1, annotate: Callable[[Node, int], str] = None, size: float = 40, algorithm_highlights: bool = False) -> Animation: """ Draw an animated spring layout graph. alpha: float in range 0 - 1 for the opacity of points color_by: function to represent a node as a single float which will be used to color it color_map: string name of matplotlib.pyplot.cm to take colors from when coloring using color_by interval: minimum milliseconds between updating nodes (usually capped by algorithm performance) """ # Color nodes and remove axis colors, cmap = self._get_colors(color_by, color_map) plt.axis('off') pos = np.zeros((len(self.spring_layout.nodes), 2)) fig = plt.gcf() ax = plt.gca() scatter = ax.scatter(pos[:, 0], pos[:, 1], s=size, alpha=alpha, c=colors, cmap=cmap) def update_nodes(frame): pos = self.spring_layout.spring_layout(return_after=draw_every) scatter.set_offsets(pos) ax.set_xlim(*self._get_limits(pos[:, 0], delta=5)) ax.set_ylim(*self._get_limits(pos[:, 1], delta=5)) return scatter, if annotate is not None: self._enable_annotation(annotate=annotate, scatter=scatter, color_by=color_by) if algorithm_highlights: self._enable_highlights(scatter=scatter) self.ani = Animation(fig, update_nodes, interval=interval, save_count=self.spring_layout.iterations) return self.ani
def _draw_next_frame(self, *args): # carry on if there's nothing to draw right now if self._data: Animation._draw_next_frame(self,*args)
def _draw_next_frame(self, *args): # carry on if there's nothing to draw right now if self._data: Animation._draw_next_frame(self, *args)