From 09680525661579bc7d6064a76e2187a0cfbc7187 Mon Sep 17 00:00:00 2001 From: nyanpasu64 Date: Wed, 15 Aug 2018 17:26:20 -0700 Subject: [PATCH] Switch from per-channel to global background color Figure-global backgrounds are reasonably fast, while axes-specific background color causes performance drop. --- ovgenpy/channel.py | 1 - ovgenpy/renderer.py | 16 +++++++--------- 2 files changed, 7 insertions(+), 10 deletions(-) diff --git a/ovgenpy/channel.py b/ovgenpy/channel.py index 35ae84d..7bceb8a 100644 --- a/ovgenpy/channel.py +++ b/ovgenpy/channel.py @@ -18,7 +18,6 @@ class ChannelConfig: render_width_ratio: int = 1 ampl_ratio: float = 1.0 # TODO use amplification = None instead? - bg_color: Any = None line_color: Any = None diff --git a/ovgenpy/renderer.py b/ovgenpy/renderer.py index f1dfd07..b024e5f 100644 --- a/ovgenpy/renderer.py +++ b/ovgenpy/renderer.py @@ -18,12 +18,13 @@ if TYPE_CHECKING: from ovgenpy.channel import ChannelConfig -@register_config(always_dump='init_bg_color init_line_color line_width') + +@register_config(always_dump='bg_color init_line_color line_width') class RendererConfig: width: int height: int - init_bg_color: Any = 'black' + bg_color: Any = 'black' init_line_color: Any = 'white' # line_width: Optional[float] = None # TODO @@ -63,7 +64,6 @@ class MatplotlibRenderer: self.axes: List['Axes'] = None # set by set_layout() self.lines: List['Line2D'] = None # set by render_frame() first call - self.bg_colors: List = [None] * nplots self.line_colors: List = [None] * nplots self._set_layout() # mutates self @@ -81,7 +81,7 @@ class MatplotlibRenderer: # https://matplotlib.org/api/_as_gen/matplotlib.pyplot.subplots.html if self.fig: raise Exception("I don't currently expect to call set_layout() twice") - plt.close(self.fig) + # plt.close(self.fig) axes2d: np.ndarray['Axes'] self.fig, axes2d = plt.subplots( @@ -93,7 +93,7 @@ class MatplotlibRenderer: # remove Axis from Axes for ax in axes2d.flatten(): - ax.set_axis_off() # FIXME ax.get_xaxis().set_visible(False) + ax.set_axis_off() # Generate arrangement (using nplots, cfg.orientation) self.axes = self.layout.arrange(lambda row, col: axes2d[row, col]) @@ -112,8 +112,6 @@ class MatplotlibRenderer: raise ValueError( f"cannot assign {len(channel_cfgs)} colors to {self.nplots} plots" ) - self.bg_colors = [coalesce(cfg.bg_color, self.cfg.init_bg_color) - for cfg in channel_cfgs] self.line_colors = [coalesce(cfg.line_color, self.cfg.init_line_color) for cfg in channel_cfgs] @@ -125,17 +123,17 @@ class MatplotlibRenderer: # Initialize axes and draw waveform data if self.lines is None: + self.fig.set_facecolor(self.cfg.bg_color) + self.lines = [] for idx, data in enumerate(datas): # Setup colors - bg_color = self.bg_colors[idx] line_color = self.line_colors[idx] # Setup axes ax = self.axes[idx] ax.set_xlim(0, len(data) - 1) ax.set_ylim(-1, 1) - ax.set_facecolor(bg_color) # Plot line line = ax.plot(data, color=line_color)[0]