def get_rgb(self, function): """ function(pylab) """ if self.figure is None: self.init_figure() from . import pylab, pylab2rgb pylab.figure(self.figure.number) function(pylab) # http://matplotlib.sourceforge.net/users/tight_layout_guide.html try: pylab.tight_layout() except Exception as e: msg = ('Could not call tight_layout(); available only on ' 'Matplotlib >=1.1 (%s)' % e) if not self.warned: warnings.warn(msg) self.warned = True # There is a bug that makes the image smaller than desired # if tight is True pixel_data = pylab2rgb(transparent=self.transparent, tight=self.tight) from procgraph_images import image_pad # need here otherwise circular # So we check and compensate shape = pixel_data.shape[0:2] shape_expected = (self.height, self.width) if shape != shape_expected: msg = ('pylab2rgb() returned size %s instead of %s.' % (shape, shape_expected)) msg += ' I will pad the image with white.' warnings.warn(msg) pixel_data = image_pad(pixel_data, shape_expected, bgcolor=[1, 1, 1]) assert_equal(pixel_data.shape[0:2], shape_expected) if not self.keep: pylab.close(self.figure.number) self.figure = None else: pass # pylab.cla() # pylab.clf() return pixel_data
def update(self): self.limits = None start = time.clock() if self.figure is None: self.init_figure() pylab.figure(self.figure.number) self.apply_styles(pylab) for i in range(self.num_input_signals()): value = self.input[i] if value is None: raise BadInput('Input is None (did you forget a |sync|?)', self, i) elif isinstance(value, tuple): if len(value) != 2: raise BadInput( 'Expected tuple of length 2 instead of %d.' % len(value), self, i) xo = value[0] yo = value[1] if xo is None or yo is None: raise BadInput('Invalid members of tuple', self, i) x = np.array(xo) y = np.array(yo) # X must be one-dimensional if len(x.shape) > 1: raise BadInput('Bad x vector w/shape %s.' % str(x.shape), self, i) # y should be of dimensions ...? if len(y.shape) > 2: raise BadInput('Bad shape for y vector %s.' % str(y.shape), self, i) if len(x) != y.shape[0]: raise BadInput('Incompatible dimensions x: %s, y: %s' % (str(x.shape), str(y.shape))) # TODO: check x unidimensional (T) # TODO: check y compatible dimensions (T x N) else: y = np.array(value) if len(y.shape) > 2: raise BadInput('Bad shape for y vector %s.' % str(y.shape), self, i) if len(y.shape) == 1: x = np.array(range(len(y))) else: assert (len(y.shape) == 2) x = np.array(range(y.shape[1])) if len(x) <= 1: continue if len(y.shape) == 2: y = y.transpose() if len(y.shape) == 1: pid = self.canonicalize_input(i) self.plot_one(pid, x, y, self.config.format) else: assert (len(y.shape) == 2) num_lines = y.shape[0] for k in range(num_lines): pid = "%s-%d" % (self.canonicalize_input(i), k) yk = y[k, :] self.plot_one(pid, x, yk, self.config.format) # TODO: check that if one has time vector, also others have it if self.limits is not None: if self.config.x_min is not None: self.limits[0] = self.config.x_min if self.config.x_max is not None: self.limits[1] = self.config.x_max if self.config.y_min is not None: self.limits[2] = self.config.y_min if self.config.y_max is not None: self.limits[3] = self.config.y_max if self.config.symmetric: if self.config.y_min is not None or \ self.config.y_max is not None: raise BadConfig( 'Cannot specify symmetric together with' 'y_min or y_max.', self, 'symmetric') M = max(abs(self.limits[2:4])) if 'dottedzero' in self.config.fancy_styles: a = pylab.axis() pylab.plot([a[0], a[1]], [0, 0], 'k--') self.limits[2] = -M self.limits[3] = M # leave some space above and below self.limits[2] = self.limits[2] * 1.1 self.limits[3] = self.limits[3] * 1.1 self.axes.axis(self.limits) if self.legend_handle is None: legend = self.config.legend if legend: self.legend_handle = self.axes.legend(*legend, loc='upper right', handlelength=1.5, markerscale=2, labelspacing=0.03, borderpad=0, handletextpad=0.03, borderaxespad=1) # http://matplotlib.sourceforge.net/users/tight_layout_guide.html try: pylab.tight_layout() except Exception as e: msg = ('Could not call tight_layout(); available only on ' 'Matplotlib >=1.1 (%s)' % e) if not self.warned: self.warning(msg) self.warned = True plotting = time.clock() - start start = time.clock() # There is a bug that makes the image smaller than desired # if tight is True pixel_data = pylab2rgb(transparent=self.config.transparent, tight=self.config.tight) # So we check and compensate shape = pixel_data.shape[0:2] shape_expected = (self.config.height, self.config.width) from procgraph_images import image_pad # need here, otherwise circular if shape != shape_expected: msg = ('pylab2rgb() returned size %s instead of %s.' % (shape, shape_expected)) msg += ' I will pad the image with white.' self.warning(msg) pixel_data = image_pad(pixel_data, shape_expected, bgcolor=[1, 1, 1]) assert_equal(pixel_data.shape[0:2], shape_expected) reading = time.clock() - start if False: # 30 49 30 before print("plotting: %dms reading: %dms" % (plotting * 1000, reading * 1000)) self.output.rgb = pixel_data if not self.config.keep: pylab.close(self.figure.number) self.figure = None
def update(self): self.limits = None start = time.clock() if self.figure is None: self.init_figure() pylab.figure(self.figure.number) self.apply_styles(pylab) for i in range(self.num_input_signals()): value = self.input[i] if value is None: raise BadInput('Input is None (did you forget a |sync|?)', self, i) elif isinstance(value, tuple): if len(value) != 2: raise BadInput( 'Expected tuple of length 2 instead of %d.' % len(value), self, i) xo = value[0] yo = value[1] if xo is None or yo is None: raise BadInput('Invalid members of tuple', self, i) x = numpy.array(xo) y = numpy.array(yo) # X must be one-dimensional if len(x.shape) > 1: raise BadInput('Bad x vector w/shape %s.' % str(x.shape), self, i) # y should be of dimensions ...? if len(y.shape) > 2: raise BadInput('Bad shape for y vector %s.' % str(y.shape), self, i) if len(x) != y.shape[0]: raise BadInput('Incompatible dimensions x: %s, y: %s' % (str(x.shape), str(y.shape))) # TODO: check x unidimensional (T) # TODO: check y compatible dimensions (T x N) else: y = numpy.array(value) if len(y.shape) > 2: raise BadInput('Bad shape for y vector %s.' % str(y.shape), self, i) if len(y.shape) == 1: x = numpy.array(range(len(y))) else: assert(len(y.shape) == 2) x = numpy.array(range(y.shape[1])) if len(x) <= 1: continue if len(y.shape) == 2: y = y.transpose() if len(y.shape) == 1: pid = self.canonicalize_input(i) self.plot_one(pid, x, y, self.config.format) else: assert(len(y.shape) == 2) num_lines = y.shape[0] for k in range(num_lines): pid = "%s-%d" % (self.canonicalize_input(i), k) yk = y[k, :] self.plot_one(pid, x, yk, self.config.format) # TODO: check that if one has time vector, also others have it if self.limits is not None: if self.config.x_min is not None: self.limits[0] = self.config.x_min if self.config.x_max is not None: self.limits[1] = self.config.x_max if self.config.y_min is not None: self.limits[2] = self.config.y_min if self.config.y_max is not None: self.limits[3] = self.config.y_max if self.config.symmetric: if self.config.y_min is not None or \ self.config.y_max is not None: raise BadConfig('Cannot specify symmetric together with' 'y_min or y_max.', self, 'symmetric') M = max(abs(self.limits[2:4])) if 'dottedzero' in self.config.fancy_styles: a = pylab.axis() pylab.plot([a[0], a[1]], [0, 0], 'k--') self.limits[2] = -M self.limits[3] = M # leave some space above and below self.limits[2] = self.limits[2] * 1.1 self.limits[3] = self.limits[3] * 1.1 self.axes.axis(self.limits) if self.legend_handle is None: legend = self.config.legend if legend: self.legend_handle = self.axes.legend(*legend, loc='upper right', handlelength=1.5, markerscale=2, labelspacing=0.03, borderpad=0, handletextpad=0.03, borderaxespad=1) # http://matplotlib.sourceforge.net/users/tight_layout_guide.html try: pylab.tight_layout() except Exception as e: msg = ('Could not call tight_layout(); available only on ' 'Matplotlib >=1.1 (%s)' % e) if not self.warned: self.warning(msg) self.warned = True plotting = time.clock() - start start = time.clock() # There is a bug that makes the image smaller than desired # if tight is True pixel_data = pylab2rgb(transparent=self.config.transparent, tight=self.config.tight) # So we check and compensate shape = pixel_data.shape[0:2] shape_expected = (self.config.height, self.config.width) if shape != shape_expected: msg = ('pylab2rgb() returned size %s instead of %s.' % (shape, shape_expected)) msg += ' I will pad the image with white.' self.warning(msg) pixel_data = image_pad(pixel_data, shape_expected, bgcolor=[1, 1, 1]) assert_equal(pixel_data.shape[0:2], shape_expected) reading = time.clock() - start if False: # 30 49 30 before print("plotting: %dms reading: %dms" % (plotting * 1000, reading * 1000)) self.output.rgb = pixel_data if not self.config.keep: pylab.close(self.figure.number) self.figure = None