class PanelColourBar(wx.Panel): def __init__(self, parent, colourMap): wx.Panel.__init__(self, parent) dpi = wx.ScreenDC().GetPPI()[0] figure = matplotlib.figure.Figure(facecolor='white', dpi=dpi) figure.set_size_inches(200.0 / dpi, 25.0 / dpi) self.canvas = FigureCanvas(self, -1, figure) axes = figure.add_subplot(111) figure.subplots_adjust(0, 0, 1, 1) norm = Normalize(vmin=0, vmax=1) self.bar = ColorbarBase(axes, norm=norm, orientation='horizontal', cmap=cm.get_cmap(colourMap)) axes.xaxis.set_visible(False) def set_map(self, colourMap): self.bar.set_cmap(colourMap) self.bar.changed() self.bar.draw_all() self.canvas.draw()
class PanelColourBar(wx.Panel): def __init__(self, parent, colourMap): wx.Panel.__init__(self, parent) dpi = wx.ScreenDC().GetPPI()[0] figure = matplotlib.figure.Figure(facecolor='white', dpi=dpi) figure.set_size_inches(200.0 / dpi, 25.0 / dpi) self.canvas = FigureCanvas(self, -1, figure) axes = figure.add_subplot(111) figure.subplots_adjust(0, 0, 1, 1) norm = Normalize(vmin=0, vmax=1) self.bar = ColorbarBase(axes, norm=norm, orientation='horizontal', cmap=cm.get_cmap(colourMap)) axes.xaxis.set_visible(False) def set_map(self, colourMap): self.bar.set_cmap(colourMap) self.bar.changed() self.bar.draw_all() self.canvas.draw()
class Plotter(object): def __init__(self, notify, figure, settings): self.notify = notify self.figure = figure self.settings = settings self.axes = None self.bar = None self.barBase = None self.threadPlot = None self.extent = None self.lines = {} self.labels = {} self.overflowLabels = {} self.overflow = {'left': [], 'right': [], 'top': [], 'bottom': []} self.__setup_plot() self.set_grid(self.settings.grid) def __setup_plot(self): formatter = ScalarFormatter(useOffset=False) gs = GridSpec(1, 2, width_ratios=[9.5, 0.5]) self.axes = self.figure.add_subplot(gs[0], axisbg=self.settings.background) self.axes.set_xlabel("Frequency (MHz)") self.axes.set_ylabel('Level ($\mathsf{dB/\sqrt{Hz}}$)') self.axes.xaxis.set_major_formatter(formatter) self.axes.yaxis.set_major_formatter(formatter) self.axes.xaxis.set_minor_locator(AutoMinorLocator(10)) self.axes.yaxis.set_minor_locator(AutoMinorLocator(10)) self.axes.set_xlim(self.settings.start, self.settings.stop) self.axes.set_ylim(-50, 0) self.bar = self.figure.add_subplot(gs[1]) norm = Normalize(vmin=-50, vmax=0) self.barBase = ColorbarBase(self.bar, norm=norm) self.set_colourmap_use(self.settings.colourMapUse) self.__setup_measure() self.__setup_overflow() self.hide_measure() def __setup_measure(self): dashesAvg = [4, 5, 1, 5, 1, 5] dashesGM = [5, 5, 5, 5, 1, 5, 1, 5] dashesHalf = [1, 5, 5, 5, 5, 5] self.lines[Markers.MIN] = Line2D([0, 0], [0, 0], linestyle='--', color='black') self.lines[Markers.MAX] = Line2D([0, 0], [0, 0], linestyle='-.', color='black') self.lines[Markers.AVG] = Line2D([0, 0], [0, 0], dashes=dashesAvg, color='magenta') self.lines[Markers.GMEAN] = Line2D([0, 0], [0, 0], dashes=dashesGM, color='green') self.lines[Markers.HP] = Line2D([0, 0], [0, 0], dashes=dashesHalf, color='purple') self.lines[Markers.HFS] = Line2D([0, 0], [0, 0], dashes=dashesHalf, color='purple') self.lines[Markers.HFE] = Line2D([0, 0], [0, 0], dashes=dashesHalf, color='purple') self.lines[Markers.OP] = Line2D([0, 0], [0, 0], dashes=dashesHalf, color='#996600') self.lines[Markers.OFS] = Line2D([0, 0], [0, 0], dashes=dashesHalf, color='#996600') self.lines[Markers.OFE] = Line2D([0, 0], [0, 0], dashes=dashesHalf, color='#996600') if matplotlib.__version__ >= '1.3': effect = patheffects.withStroke(linewidth=3, foreground="w", alpha=0.75) self.lines[Markers.MIN].set_path_effects([effect]) self.lines[Markers.MAX].set_path_effects([effect]) self.lines[Markers.AVG].set_path_effects([effect]) self.lines[Markers.GMEAN].set_path_effects([effect]) self.lines[Markers.HP].set_path_effects([effect]) self.lines[Markers.HFS].set_path_effects([effect]) self.lines[Markers.HFE].set_path_effects([effect]) self.lines[Markers.OP].set_path_effects([effect]) self.lines[Markers.OFS].set_path_effects([effect]) self.lines[Markers.OFE].set_path_effects([effect]) for line in self.lines.itervalues(): self.axes.add_line(line) bbox = self.axes.bbox box = dict(boxstyle='round', fc='white', ec='black', clip_box=bbox) self.labels[Markers.MIN] = Text(0, 0, 'Min', fontsize='xx-small', ha="right", va="bottom", bbox=box, color='black') self.labels[Markers.MAX] = Text(0, 0, 'Max', fontsize='xx-small', ha="right", va="top", bbox=box, color='black') box['ec'] = 'magenta' self.labels[Markers.AVG] = Text(0, 0, 'Mean', fontsize='xx-small', ha="right", va="center", bbox=box, color='magenta') box['ec'] = 'green' self.labels[Markers.GMEAN] = Text(0, 0, 'GMean', fontsize='xx-small', ha="right", va="center", bbox=box, color='green') box['ec'] = 'purple' self.labels[Markers.HP] = Text(0, 0, '-3dB', fontsize='xx-small', ha="right", va="center", bbox=box, color='purple') self.labels[Markers.HFS] = Text(0, 0, '-3dB Start', fontsize='xx-small', ha="center", va="top", bbox=box, color='purple') self.labels[Markers.HFE] = Text(0, 0, '-3dB End', fontsize='xx-small', ha="center", va="top", bbox=box, color='purple') box['ec'] = '#996600' self.labels[Markers.OP] = Text(0, 0, 'OBW', fontsize='xx-small', ha="right", va="center", bbox=box, color='#996600') self.labels[Markers.OFS] = Text(0, 0, 'OBW Start', fontsize='xx-small', ha="center", va="top", bbox=box, color='#996600') self.labels[Markers.OFE] = Text(0, 0, 'OBW End', fontsize='xx-small', ha="center", va="top", bbox=box, color='#996600') for label in self.labels.itervalues(): self.axes.add_artist(label) def __setup_overflow(self): bbox = self.axes.bbox box = dict(boxstyle='round', fc='white', ec='black', alpha=0.5, clip_box=bbox) self.overflowLabels['left'] = Text(0, 0.9, '', fontsize='xx-small', ha="left", va="top", bbox=box, transform=self.axes.transAxes, alpha=0.5) self.overflowLabels['right'] = Text(1, 0.9, '', fontsize='xx-small', ha="right", va="top", bbox=box, transform=self.axes.transAxes, alpha=0.5) self.overflowLabels['top'] = Text(0.9, 1, '', fontsize='xx-small', ha="right", va="top", bbox=box, transform=self.axes.transAxes, alpha=0.5) self.overflowLabels['bottom'] = Text(0.9, 0, '', fontsize='xx-small', ha="right", va="bottom", bbox=box, transform=self.axes.transAxes, alpha=0.5) for label in self.overflowLabels.itervalues(): self.axes.add_artist(label) def __clear_overflow(self): for label in self.overflowLabels: self.overflow[label] = [] def __draw_hline(self, marker, y): line = self.lines[marker] label = self.labels[marker] xLim = self.axes.get_xlim() yLim = self.axes.get_ylim() if yLim[0] <= y <= yLim[1]: line.set_visible(True) line.set_xdata([xLim[0], xLim[1]]) line.set_ydata([y, y]) self.axes.draw_artist(line) label.set_visible(True) label.set_position((xLim[1], y)) self.axes.draw_artist(label) elif y is not None and y < yLim[0]: self.overflow['bottom'].append(marker) elif y is not None and y > yLim[1]: self.overflow['top'].append(marker) def __draw_vline(self, marker, x): line = self.lines[marker] label = self.labels[marker] yLim = self.axes.get_ylim() xLim = self.axes.get_xlim() if xLim[0] <= x <= xLim[1]: line.set_visible(True) line.set_xdata([x, x]) line.set_ydata([yLim[0], yLim[1]]) self.axes.draw_artist(line) label.set_visible(True) label.set_position((x, yLim[1])) self.axes.draw_artist(label) elif x is not None and x < xLim[0]: self.overflow['left'].append(marker) elif x is not None and x > xLim[1]: self.overflow['right'].append(marker) def __draw_overflow(self): for pos, overflow in self.overflow.iteritems(): if len(overflow) > 0: text = '' for measure in overflow: if len(text) > 0: text += '\n' text += self.labels[measure].get_text() label = self.overflowLabels[pos] if pos == 'top': textMath = '$\\blacktriangle$\n' + text elif pos == 'bottom': textMath = '$\\blacktriangledown$\n' + text elif pos == 'left': textMath = '$\\blacktriangleleft$\n' + text elif pos == 'right': textMath = '$\\blacktriangleright$\n' + text label.set_text(textMath) label.set_visible(True) self.axes.draw_artist(label) def draw_measure(self, measure, show): if self.axes.get_renderer_cache() is None: return self.hide_measure() self.__clear_overflow() if show[Measure.MIN]: y = measure.get_min_p()[1] self.__draw_hline(Markers.MIN, y) if show[Measure.MAX]: y = measure.get_max_p()[1] self.__draw_hline(Markers.MAX, y) if show[Measure.AVG]: y = measure.get_avg_p() self.__draw_hline(Markers.AVG, y) if show[Measure.GMEAN]: y = measure.get_gmean_p() self.__draw_hline(Markers.GMEAN, y) if show[Measure.HBW]: xStart, xEnd, y = measure.get_hpw() self.__draw_hline(Markers.HP, y) self.__draw_vline(Markers.HFS, xStart) self.__draw_vline(Markers.HFE, xEnd) if show[Measure.OBW]: xStart, xEnd, y = measure.get_obw() self.__draw_hline(Markers.OP, y) self.__draw_vline(Markers.OFE, xStart) self.__draw_vline(Markers.OFE, xEnd) self.__draw_overflow() def hide_measure(self): for line in self.lines.itervalues(): line.set_visible(False) for label in self.labels.itervalues(): label.set_visible(False) for label in self.overflowLabels.itervalues(): label.set_visible(False) def scale_plot(self, force=False): if self.extent is not None: if self.settings.autoF or force: self.axes.set_xlim(self.extent.get_f()) if self.settings.autoL or force: self.axes.set_ylim(self.extent.get_l()) if self.settings.plotFunc == PlotFunc.VAR and len( self.axes.collections) > 0: norm = self.axes.collections[0].norm self.barBase.set_clim((norm.vmin, norm.vmax)) else: self.barBase.set_clim(self.extent.get_l()) norm = Normalize(vmin=self.extent.get_l()[0], vmax=self.extent.get_l()[1]) for collection in self.axes.collections: collection.set_norm(norm) try: self.barBase.draw_all() except: pass def redraw_plot(self): if self.figure is not None: post_event(self.notify, EventThread(Event.DRAW)) def get_axes(self): return self.axes def get_axes_bar(self): return self.barBase.ax def get_plot_thread(self): return self.threadPlot def set_title(self, title): self.axes.set_title(title, fontsize='medium') def set_plot(self, spectrum, extent, annotate=False): self.extent = extent self.threadPlot = ThreadPlot(self, self.settings, self.axes, spectrum, self.extent, self.barBase, annotate) self.threadPlot.start() return self.threadPlot def clear_plots(self): children = self.axes.get_children() for child in children: if child.get_gid() is not None: if child.get_gid() in [ 'plot', 'peak', 'peakText', 'peakShadow', 'peakThres' ]: child.remove() def set_grid(self, on): self.axes.grid(on) self.redraw_plot() def set_bar(self, on): self.barBase.ax.set_visible(on) if on: self.axes.change_geometry(1, 2, 1) self.axes.get_subplotspec().get_gridspec().set_width_ratios( [9.5, 0.5]) else: self.axes.change_geometry(1, 1, 1) self.figure.subplots_adjust() def set_axes(self, on): if on: self.axes.set_axis_on() self.bar.set_axis_on() else: self.axes.set_axis_off() self.bar.set_axis_off() def set_colourmap_use(self, on): self.set_bar(on) if on: colourMap = self.settings.colourMap else: colourMap = get_colours()[0] self.set_colourmap(colourMap) def set_colourmap(self, colourMap): self.settings.colourMap = colourMap for collection in self.axes.collections: collection.set_cmap(colourMap) if get_colours().index(colourMap) < 4: self.set_bar(False) else: self.set_bar(True) self.barBase.set_cmap(colourMap) try: self.barBase.draw_all() except: pass def close(self): self.figure.clear() self.figure = None
def draw(self): PlotBase.draw(self) self.x_formatter_cb(self.ax) if self.gdata.isEmpty(): return None # Evaluate the bar width width = float(self.width) offset = 0.0 if self.gdata.key_type == "time": width = width / 86400.0 start_plot = 0 end_plot = 0 if "starttime" in self.prefs and "endtime" in self.prefs: start_plot = date2num( datetime.datetime.fromtimestamp( to_timestamp(self.prefs["starttime"]))) end_plot = date2num( datetime.datetime.fromtimestamp( to_timestamp(self.prefs["endtime"]))) labels = self.gdata.getLabels() nKeys = self.gdata.getNumberOfKeys() tmp_b = [] tmp_x = [] tmp_y = [] self.bars = [] labels = self.gdata.getLabels() nLabel = 0 labelNames = [] colors = [] xmin = None xmax = None for label, _num in labels: labelNames.append(label) for key, value, _error in self.gdata.getPlotNumData(label): if xmin is None or xmin > (key + offset): xmin = key + offset if xmax is None or xmax < (key + offset): xmax = key + offset if value is not None: colors.append(self.getQualityColor(value)) tmp_x.append(key + offset) tmp_y.append(1.0) tmp_b.append(float(nLabel)) nLabel += 1 self.bars += self.ax.bar(tmp_x, tmp_y, bottom=tmp_b, width=width, color=colors) dpi = self.prefs.get("dpi", 100) setp(self.bars, linewidth=pixelToPoint(0.5, dpi), edgecolor="#AAAAAA") # pivots = keys # for idx in range(len(pivots)): # self.coords[ pivots[idx] ] = self.bars[idx] ymax = float(nLabel) self.ax.set_xlim(xmin=0.0, xmax=xmax + width + offset) self.ax.set_ylim(ymin=0.0, ymax=ymax) if self.gdata.key_type == "time": if start_plot and end_plot: self.ax.set_xlim(xmin=start_plot, xmax=end_plot) else: self.ax.set_xlim(xmin=min(tmp_x), xmax=max(tmp_x)) self.ax.set_yticks([i + 0.5 for i in range(nLabel)]) self.ax.set_yticklabels(labelNames) setp(self.ax.get_xticklines(), markersize=0.0) setp(self.ax.get_yticklines(), markersize=0.0) cax, kw = make_axes(self.ax, orientation="vertical", fraction=0.07) cb = ColorbarBase(cax, cmap=self.cmap, norm=self.norms, boundaries=self.cbBoundaries, values=self.cbValues, ticks=self.cbTicks) cb.draw_all()
class Spectrogram(object): def __init__(self, notify, figure, settings): self.notify = notify self.figure = figure self.settings = settings self.data = [[], [], []] self.axes = None self.plot = None self.extent = None self.bar = None self.barBase = None self.lines = {} self.labels = {} self.overflowLabels = {} self.overflow = {'left': [], 'right': []} self.threadPlot = None self.__setup_plot() self.set_grid(self.settings.grid) def __setup_plot(self): gs = GridSpec(1, 2, width_ratios=[9.5, 0.5]) self.axes = self.figure.add_subplot(gs[0], axisbg=self.settings.background) self.axes.set_xlabel("Frequency (MHz)") self.axes.set_ylabel('Time') numFormatter = ScalarFormatter(useOffset=False) timeFormatter = DateFormatter("%H:%M:%S") self.axes.xaxis.set_major_formatter(numFormatter) self.axes.yaxis.set_major_formatter(timeFormatter) self.axes.xaxis.set_minor_locator(AutoMinorLocator(10)) self.axes.yaxis.set_minor_locator(AutoMinorLocator(10)) self.axes.set_xlim(self.settings.start, self.settings.stop) now = time.time() self.axes.set_ylim(utc_to_mpl(now), utc_to_mpl(now - 10)) self.bar = self.figure.add_subplot(gs[1]) norm = Normalize(vmin=-50, vmax=0) self.barBase = ColorbarBase(self.bar, norm=norm, cmap=cm.get_cmap(self.settings.colourMap)) self.__setup_measure() self.__setup_overflow() self.hide_measure() def __setup_measure(self): dashesHalf = [1, 5, 5, 5, 5, 5] self.lines[Markers.HFS] = Line2D([0, 0], [0, 0], dashes=dashesHalf, color='purple') self.lines[Markers.HFE] = Line2D([0, 0], [0, 0], dashes=dashesHalf, color='purple') self.lines[Markers.OFS] = Line2D([0, 0], [0, 0], dashes=dashesHalf, color='#996600') self.lines[Markers.OFE] = Line2D([0, 0], [0, 0], dashes=dashesHalf, color='#996600') if matplotlib.__version__ >= '1.3': effect = patheffects.withStroke(linewidth=3, foreground="w", alpha=0.75) self.lines[Markers.HFS].set_path_effects([effect]) self.lines[Markers.HFE].set_path_effects([effect]) self.lines[Markers.OFS].set_path_effects([effect]) self.lines[Markers.OFE].set_path_effects([effect]) for line in self.lines.itervalues(): self.axes.add_line(line) bbox = self.axes.bbox box = dict(boxstyle='round', fc='white', ec='purple', clip_box=bbox) self.labels[Markers.HFS] = Text(0, 0, '-3dB', fontsize='xx-small', ha="center", va="top", bbox=box, color='purple') self.labels[Markers.HFE] = Text(0, 0, '-3dB', fontsize='xx-small', ha="center", va="top", bbox=box, color='purple') box['ec'] = '#996600' self.labels[Markers.OFS] = Text(0, 0, 'OBW', fontsize='xx-small', ha="center", va="top", bbox=box, color='#996600') self.labels[Markers.OFE] = Text(0, 0, 'OBW', fontsize='xx-small', ha="center", va="top", bbox=box, color='#996600') for label in self.labels.itervalues(): self.axes.add_artist(label) def __setup_overflow(self): bbox = self.axes.bbox box = dict(boxstyle='round', fc='white', ec='black', alpha=0.5, clip_box=bbox) self.overflowLabels['left'] = Text(0, 0.9, '', fontsize='xx-small', ha="left", va="top", bbox=box, transform=self.axes.transAxes, alpha=0.5) self.overflowLabels['right'] = Text(1, 0.9, '', fontsize='xx-small', ha="right", va="top", bbox=box, transform=self.axes.transAxes, alpha=0.5) for label in self.overflowLabels.itervalues(): self.axes.add_artist(label) def __clear_overflow(self): for label in self.overflowLabels: self.overflow[label] = [] def __draw_vline(self, marker, x): line = self.lines[marker] label = self.labels[marker] yLim = self.axes.get_ylim() xLim = self.axes.get_xlim() if xLim[0] < x < xLim[1]: line.set_visible(True) line.set_xdata([x, x]) line.set_ydata([yLim[0], yLim[1]]) self.axes.draw_artist(line) label.set_visible(True) label.set_position((x, yLim[1])) self.axes.draw_artist(label) elif x is not None and x < xLim[0]: self.overflow['left'].append(marker) elif x is not None and x > xLim[1]: self.overflow['right'].append(marker) def __draw_overflow(self): for pos, overflow in self.overflow.iteritems(): if len(overflow) > 0: text = '' for measure in overflow: if len(text) > 0: text += '\n' text += self.labels[measure].get_text() label = self.overflowLabels[pos] if pos == 'left': textMath = '$\\blacktriangleleft$\n' + text elif pos == 'right': textMath = '$\\blacktriangleright$\n' + text label.set_text(textMath) label.set_visible(True) self.axes.draw_artist(label) def draw_measure(self, measure, show): if self.axes.get_renderer_cache() is None: return self.hide_measure() self.__clear_overflow() if show[Measure.HBW]: xStart, xEnd, _y = measure.get_hpw() self.__draw_vline(Markers.HFS, xStart) self.__draw_vline(Markers.HFE, xEnd) if show[Measure.OBW]: xStart, xEnd, _y = measure.get_obw() self.__draw_vline(Markers.OFS, xStart) self.__draw_vline(Markers.OFE, xEnd) self.__draw_overflow() def hide_measure(self): for line in self.lines.itervalues(): line.set_visible(False) for label in self.labels.itervalues(): label.set_visible(False) for label in self.overflowLabels.itervalues(): label.set_visible(False) def scale_plot(self, force=False): if self.figure is not None and self.plot is not None: extent = self.plot.get_extent() if self.settings.autoF or force: if extent[0] == extent[1]: extent[1] += 1 self.axes.set_xlim(extent[0], extent[1]) if self.settings.autoL or force: vmin, vmax = self.plot.get_clim() self.barBase.set_clim(vmin, vmax) try: self.barBase.draw_all() except: pass if self.settings.autoT or force: self.axes.set_ylim(extent[2], extent[3]) def redraw_plot(self): if self.figure is not None: post_event(self.notify, EventThread(Event.DRAW)) def get_axes(self): return self.axes def get_axes_bar(self): return self.barBase.ax def get_plot_thread(self): return self.threadPlot def set_title(self, title): self.axes.set_title(title, fontsize='medium') def set_plot(self, spectrum, extent, annotate=False): self.extent = extent self.threadPlot = ThreadPlot(self, self.settings, self.axes, spectrum, self.extent, self.barBase, annotate) self.threadPlot.start() def clear_plots(self): children = self.axes.get_children() for child in children: if child.get_gid() is not None: if child.get_gid() == "plot_line" or child.get_gid() == "peak": child.remove() def set_grid(self, on): if on: self.axes.grid(True, color='w') else: self.axes.grid(False) self.redraw_plot() def set_colourmap(self, colourMap): if self.plot is not None: self.plot.set_cmap(colourMap) self.barBase.set_cmap(colourMap) try: self.barBase.draw_all() except: pass def close(self): self.figure.clear() self.figure = None
class Plotter3d(): def __init__(self, notify, figure, settings): self.notify = notify self.figure = figure self.settings = settings self.axes = None self.bar = None self.barBase = None self.plot = None self.extent = None self.threadPlot = None self.wireframe = settings.wireframe self.__setup_plot() self.set_grid(settings.grid) def __setup_plot(self): gs = GridSpec(1, 2, width_ratios=[9.5, 0.5]) self.axes = self.figure.add_subplot(gs[0], projection='3d') numformatter = ScalarFormatter(useOffset=False) timeFormatter = DateFormatter("%H:%M:%S") self.axes.set_xlabel("Frequency (MHz)") self.axes.set_ylabel('Time') self.axes.set_zlabel('Level ($\mathsf{dB/\sqrt{Hz}}$)') colour = hex2color(self.settings.background) colour += (1,) self.axes.w_xaxis.set_pane_color(colour) self.axes.w_yaxis.set_pane_color(colour) self.axes.w_zaxis.set_pane_color(colour) self.axes.xaxis.set_major_formatter(numformatter) self.axes.yaxis.set_major_formatter(timeFormatter) self.axes.zaxis.set_major_formatter(numformatter) self.axes.xaxis.set_minor_locator(AutoMinorLocator(10)) self.axes.yaxis.set_minor_locator(AutoMinorLocator(10)) self.axes.zaxis.set_minor_locator(AutoMinorLocator(10)) self.axes.set_xlim(self.settings.start, self.settings.stop) now = time.time() self.axes.set_ylim(epoch_to_mpl(now), epoch_to_mpl(now - 10)) self.axes.set_zlim(-50, 0) self.bar = self.figure.add_subplot(gs[1]) norm = Normalize(vmin=-50, vmax=0) self.barBase = ColorbarBase(self.bar, norm=norm, cmap=cm.get_cmap(self.settings.colourMap)) def scale_plot(self, force=False): if self.extent is not None and self.plot is not None: if self.settings.autoF or force: self.axes.set_xlim(self.extent.get_f()) if self.settings.autoL or force: self.axes.set_zlim(self.extent.get_l()) self.plot.set_clim(self.extent.get_l()) self.barBase.set_clim(self.extent.get_l()) try: self.barBase.draw_all() except: pass if self.settings.autoT or force: self.axes.set_ylim(self.extent.get_t()) def draw_measure(self, *args): pass def hide_measure(self): pass def redraw_plot(self): if self.figure is not None: post_event(self.notify, EventThread(Event.DRAW)) def get_axes(self): return self.axes def get_axes_bar(self): return self.barBase.ax def get_plot_thread(self): return self.threadPlot def set_title(self, title): self.axes.set_title(title, fontsize='medium') def set_plot(self, data, extent, annotate=False): self.extent = extent self.threadPlot = ThreadPlot(self, self.axes, data, self.extent, self.settings.retainMax, self.settings.colourMap, self.settings.autoF, self.barBase, annotate) self.threadPlot.start() def clear_plots(self): children = self.axes.get_children() for child in children: if child.get_gid() is not None: if child.get_gid() == "plot" or child.get_gid() == "peak": child.remove() def set_grid(self, on): self.axes.grid(on) self.redraw_plot() def set_colourmap(self, colourMap): if self.plot is not None: self.plot.set_cmap(colourMap) self.barBase.set_cmap(colourMap) try: self.barBase.draw_all() except: pass def close(self): self.figure.clear() self.figure = None
class TopographicWidget(QtGui.QWidget): def __init__(self, ch_label, parent): super().__init__() self.parent = parent self.fig, self.axes = plt.subplots(2, 4) self.plots = list() self.colorbar_ax = self.fig.add_subplot(2, 5, 5) self.cmap = 'RdBu_r' self.norm = mpl.colors.Normalize(vmin=0, vmax=100) self.colorbar_ax.set_position([0.9, 0.04, 0.03, 0.15]) self.colorbar = ColorbarBase(self.colorbar_ax, cmap=self.cmap, norm=self.norm) self.canvas = FigureCanvas(self.fig) self.titles = [ 'Delta', 'Theta', 'Low Alpha', 'High Alpha', 'Low Beta', 'Mid Beta', 'High Beta', 'Gamma' ] hlayout = QtGui.QHBoxLayout(self) hlayout.addWidget(self.canvas) self.show() def draw(self, data, ch_label): self.pos = np.array(list(channel_dict_2D.values())) self.ch_names_ = list(channel_dict_2D.keys()) self.pos, self.outlines = topomap._check_outlines(self.pos, 'head') center = 0.5 * (self.pos.max(axis=0) + self.pos.min(axis=0)) scale = 1.0 / (self.pos.max(axis=0) - self.pos.min(axis=0)) scale[0] = scale[0] * 1.3 self.plt_idx = [self.ch_names_.index(name) for name in ch_label] ch_pos = np.array([self.pos[idx] * 5 / 6 for idx in self.plt_idx]) mask = np.ones((len(ch_pos), 1), dtype=bool) for idx in range(8): x_idx = idx // 4 y_idx = idx % 4 if len(self.plots) <= idx: self.axes[x_idx][y_idx].set_title(self.titles[idx]) if self.parent.ch_loc: marker = "." else: marker = "" im, cont, interp = mne.viz.topomap._plot_topomap( data[idx], ch_pos, axes=self.axes[x_idx][y_idx], contours=10, show=False, mask=mask, mask_params=dict(marker=marker), head_pos=dict(center=center, scale=scale * 1.1), cmap="RdBu_r", names=ch_label, show_names=self.parent.ch_loc) self.colorbar_ax.set_position([0.9, 0.04, 0.03, 0.15]) self.plots.append([im, cont, interp]) else: im, cont, interp = self.plots[idx] Zi = interp.set_values(data[idx])() im.set_data(Zi) # must be removed and re-added if len(cont.collections) > 0: tp = cont.collections[0] visible = tp.get_visible() patch_ = tp.get_clip_path() color = tp.get_color() lw = tp.get_linewidth() for tp in cont.collections[:]: tp.remove() cont = self.axes[x_idx][y_idx].contour(interp.Xi, interp.Yi, Zi, 10, colors=color, linewidths=lw) for tp in cont.collections: tp.set_visible(visible) tp.set_clip_path(patch_) self.plots[idx][1] = cont self.plots[idx][2] = interp self.fig.canvas.draw() def update_color_bar(self, scale_min, scale_max): norm = mpl.colors.Normalize(vmin=scale_min, vmax=scale_max) self.colorbar.norm = norm self.colorbar.draw_all()
class Plotter(object): def __init__(self, notify, figure, settings): self.notify = notify self.figure = figure self.settings = settings self.colourMap = self.settings.colourMap self.axes = None self.bar = None self.barBase = None self.threadPlot = None self.extent = None self.lines = {} self.labels = {} self.overflowLabels = {} self.overflow = {'left': [], 'right': [], 'top': [], 'bottom': []} self.__setup_plot() self.set_grid(self.settings.grid) def __setup_plot(self): formatter = ScalarFormatter(useOffset=False) gs = GridSpec(1, 2, width_ratios=[9.5, 0.5]) self.axes = self.figure.add_subplot(gs[0], axisbg=self.settings.background) self.axes.set_xlabel("Frequency (MHz)") self.axes.set_ylabel('Level ($\mathsf{dB/\sqrt{Hz}}$)') self.axes.xaxis.set_major_formatter(formatter) self.axes.yaxis.set_major_formatter(formatter) self.axes.xaxis.set_minor_locator(AutoMinorLocator(10)) self.axes.yaxis.set_minor_locator(AutoMinorLocator(10)) self.axes.set_xlim(self.settings.start, self.settings.stop) self.axes.set_ylim(-50, 0) self.bar = self.figure.add_subplot(gs[1]) norm = Normalize(vmin=-50, vmax=0) self.barBase = ColorbarBase(self.bar, norm=norm) self.set_colourmap_use(self.settings.colourMapUse) self.__setup_measure() self.__setup_overflow() self.hide_measure() def __setup_measure(self): dashesAvg = [4, 5, 1, 5, 1, 5] dashesGM = [5, 5, 5, 5, 1, 5, 1, 5] dashesHalf = [1, 5, 5, 5, 5, 5] self.lines[Markers.MIN] = Line2D([0, 0], [0, 0], linestyle='--', color='black') self.lines[Markers.MAX] = Line2D([0, 0], [0, 0], linestyle='-.', color='black') self.lines[Markers.AVG] = Line2D([0, 0], [0, 0], dashes=dashesAvg, color='magenta') self.lines[Markers.GMEAN] = Line2D([0, 0], [0, 0], dashes=dashesGM, color='green') self.lines[Markers.HP] = Line2D([0, 0], [0, 0], dashes=dashesHalf, color='purple') self.lines[Markers.HFS] = Line2D([0, 0], [0, 0], dashes=dashesHalf, color='purple') self.lines[Markers.HFE] = Line2D([0, 0], [0, 0], dashes=dashesHalf, color='purple') self.lines[Markers.OP] = Line2D([0, 0], [0, 0], dashes=dashesHalf, color='#996600') self.lines[Markers.OFS] = Line2D([0, 0], [0, 0], dashes=dashesHalf, color='#996600') self.lines[Markers.OFE] = Line2D([0, 0], [0, 0], dashes=dashesHalf, color='#996600') if matplotlib.__version__ >= '1.3': effect = patheffects.withStroke(linewidth=3, foreground="w", alpha=0.75) self.lines[Markers.MIN].set_path_effects([effect]) self.lines[Markers.MAX].set_path_effects([effect]) self.lines[Markers.AVG].set_path_effects([effect]) self.lines[Markers.GMEAN].set_path_effects([effect]) self.lines[Markers.HP].set_path_effects([effect]) self.lines[Markers.HFS].set_path_effects([effect]) self.lines[Markers.HFE].set_path_effects([effect]) self.lines[Markers.OP].set_path_effects([effect]) self.lines[Markers.OFS].set_path_effects([effect]) self.lines[Markers.OFE].set_path_effects([effect]) for line in self.lines.itervalues(): self.axes.add_line(line) bbox = self.axes.bbox box = dict(boxstyle='round', fc='white', ec='black', clip_box=bbox) self.labels[Markers.MIN] = Text(0, 0, 'Min', fontsize='xx-small', ha="right", va="bottom", bbox=box, color='black') self.labels[Markers.MAX] = Text(0, 0, 'Max', fontsize='xx-small', ha="right", va="top", bbox=box, color='black') box['ec'] = 'magenta' self.labels[Markers.AVG] = Text(0, 0, 'Mean', fontsize='xx-small', ha="right", va="center", bbox=box, color='magenta') box['ec'] = 'green' self.labels[Markers.GMEAN] = Text(0, 0, 'GMean', fontsize='xx-small', ha="right", va="center", bbox=box, color='green') box['ec'] = 'purple' self.labels[Markers.HP] = Text(0, 0, '-3dB', fontsize='xx-small', ha="right", va="center", bbox=box, color='purple') self.labels[Markers.HFS] = Text(0, 0, '-3dB Start', fontsize='xx-small', ha="center", va="top", bbox=box, color='purple') self.labels[Markers.HFE] = Text(0, 0, '-3dB End', fontsize='xx-small', ha="center", va="top", bbox=box, color='purple') box['ec'] = '#996600' self.labels[Markers.OP] = Text(0, 0, 'OBW', fontsize='xx-small', ha="right", va="center", bbox=box, color='#996600') self.labels[Markers.OFS] = Text(0, 0, 'OBW Start', fontsize='xx-small', ha="center", va="top", bbox=box, color='#996600') self.labels[Markers.OFE] = Text(0, 0, 'OBW End', fontsize='xx-small', ha="center", va="top", bbox=box, color='#996600') for label in self.labels.itervalues(): self.axes.add_artist(label) def __setup_overflow(self): bbox = self.axes.bbox box = dict(boxstyle='round', fc='white', ec='black', alpha=0.5, clip_box=bbox) self.overflowLabels['left'] = Text(0, 0.9, '', fontsize='xx-small', ha="left", va="top", bbox=box, transform=self.axes.transAxes, alpha=0.5) self.overflowLabels['right'] = Text(1, 0.9, '', fontsize='xx-small', ha="right", va="top", bbox=box, transform=self.axes.transAxes, alpha=0.5) self.overflowLabels['top'] = Text(0.9, 1, '', fontsize='xx-small', ha="right", va="top", bbox=box, transform=self.axes.transAxes, alpha=0.5) self.overflowLabels['bottom'] = Text(0.9, 0, '', fontsize='xx-small', ha="right", va="bottom", bbox=box, transform=self.axes.transAxes, alpha=0.5) for label in self.overflowLabels.itervalues(): self.axes.add_artist(label) def __clear_overflow(self): for label in self.overflowLabels: self.overflow[label] = [] def __draw_hline(self, marker, y): line = self.lines[marker] label = self.labels[marker] xLim = self.axes.get_xlim() yLim = self.axes.get_ylim() if yLim[0] <= y <= yLim[1]: line.set_visible(True) line.set_xdata([xLim[0], xLim[1]]) line.set_ydata([y, y]) self.axes.draw_artist(line) label.set_visible(True) label.set_position((xLim[1], y)) self.axes.draw_artist(label) elif y is not None and y < yLim[0]: self.overflow['bottom'].append(marker) elif y is not None and y > yLim[1]: self.overflow['top'].append(marker) def __draw_vline(self, marker, x): line = self.lines[marker] label = self.labels[marker] yLim = self.axes.get_ylim() xLim = self.axes.get_xlim() if xLim[0] <= x <= xLim[1]: line.set_visible(True) line.set_xdata([x, x]) line.set_ydata([yLim[0], yLim[1]]) self.axes.draw_artist(line) label.set_visible(True) label.set_position((x, yLim[1])) self.axes.draw_artist(label) elif x is not None and x < xLim[0]: self.overflow['left'].append(marker) elif x is not None and x > xLim[1]: self.overflow['right'].append(marker) def __draw_overflow(self): for pos, overflow in self.overflow.iteritems(): if len(overflow) > 0: text = '' for measure in overflow: if len(text) > 0: text += '\n' text += self.labels[measure].get_text() label = self.overflowLabels[pos] if pos == 'top': textMath = '$\\blacktriangle$\n' + text elif pos == 'bottom': textMath = '$\\blacktriangledown$\n' + text elif pos == 'left': textMath = '$\\blacktriangleleft$\n' + text elif pos == 'right': textMath = '$\\blacktriangleright$\n' + text label.set_text(textMath) label.set_visible(True) self.axes.draw_artist(label) def draw_measure(self, measure, show): if self.axes.get_renderer_cache() is None: return self.hide_measure() self.__clear_overflow() if show[Measure.MIN]: y = measure.get_min_p()[1] self.__draw_hline(Markers.MIN, y) if show[Measure.MAX]: y = measure.get_max_p()[1] self.__draw_hline(Markers.MAX, y) if show[Measure.AVG]: y = measure.get_avg_p() self.__draw_hline(Markers.AVG, y) if show[Measure.GMEAN]: y = measure.get_gmean_p() self.__draw_hline(Markers.GMEAN, y) if show[Measure.HBW]: xStart, xEnd, y = measure.get_hpw() self.__draw_hline(Markers.HP, y) self.__draw_vline(Markers.HFS, xStart) self.__draw_vline(Markers.HFE, xEnd) if show[Measure.OBW]: xStart, xEnd, y = measure.get_obw() self.__draw_hline(Markers.OP, y) self.__draw_vline(Markers.OFE, xStart) self.__draw_vline(Markers.OFE, xEnd) self.__draw_overflow() def hide_measure(self): for line in self.lines.itervalues(): line.set_visible(False) for label in self.labels.itervalues(): label.set_visible(False) for label in self.overflowLabels.itervalues(): label.set_visible(False) def scale_plot(self, force=False): if self.extent is not None: if self.settings.autoF or force: self.axes.set_xlim(self.extent.get_f()) if self.settings.autoL or force: self.axes.set_ylim(self.extent.get_l()) if self.settings.plotFunc == PlotFunc.VAR and len(self.axes.collections) > 0: norm = self.axes.collections[0].norm self.barBase.set_clim((norm.vmin, norm.vmax)) else: self.barBase.set_clim(self.extent.get_l()) norm = Normalize(vmin=self.extent.get_l()[0], vmax=self.extent.get_l()[1]) for collection in self.axes.collections: collection.set_norm(norm) try: self.barBase.draw_all() except: pass def redraw_plot(self): if self.figure is not None: post_event(self.notify, EventThread(Event.DRAW)) def get_axes(self): return self.axes def get_axes_bar(self): return self.barBase.ax def get_plot_thread(self): return self.threadPlot def set_title(self, title): self.axes.set_title(title, fontsize='medium') def set_plot(self, spectrum, extent, annotate=False): self.extent = extent self.threadPlot = ThreadPlot(self, self.settings, self.axes, spectrum, self.extent, self.barBase, annotate) self.threadPlot.start() return self.threadPlot def clear_plots(self): children = self.axes.get_children() for child in children: if child.get_gid() is not None: if child.get_gid() == "plot" or child.get_gid() == "peak": child.remove() def set_grid(self, on): self.axes.grid(on) self.redraw_plot() def set_bar(self, on): self.barBase.ax.set_visible(on) if on: self.axes.change_geometry(1, 2, 1) self.axes.get_subplotspec().get_gridspec().set_width_ratios([9.5, 0.5]) else: self.axes.change_geometry(1, 1, 1) self.figure.subplots_adjust() def set_axes(self, on): if on: self.axes.set_axis_on() self.bar.set_axis_on() else: self.axes.set_axis_off() self.bar.set_axis_off() def set_colourmap_use(self, on): self.set_bar(on) if on: colourMap = self.settings.colourMap else: colourMap = ' Pure Blue' self.set_colourmap(colourMap) def set_colourmap(self, colourMap): self.colourMap = colourMap for collection in self.axes.collections: collection.set_cmap(colourMap) if colourMap.startswith(' Pure'): self.bar.set_visible(False) else: self.bar.set_visible(True) self.barBase.set_cmap(colourMap) try: self.barBase.draw_all() except: pass def close(self): self.figure.clear() self.figure = None
class Plotter3d(object): def __init__(self, notify, figure, settings): self.notify = notify self.figure = figure self.settings = settings self.axes = None self.bar = None self.barBase = None self.plot = None self.extent = None self.threadPlot = None self.wireframe = settings.wireframe self.__setup_plot() self.set_grid(settings.grid) def __setup_plot(self): gs = GridSpec(1, 2, width_ratios=[9.5, 0.5]) self.axes = self.figure.add_subplot(gs[0], projection='3d') numformatter = ScalarFormatter(useOffset=False) timeFormatter = DateFormatter("%H:%M:%S") self.axes.set_xlabel("Frequency (MHz)") self.axes.set_ylabel('Time') self.axes.set_zlabel('Level ($\mathsf{dB/\sqrt{Hz}}$)') colour = hex2color(self.settings.background) colour += (1,) self.axes.w_xaxis.set_pane_color(colour) self.axes.w_yaxis.set_pane_color(colour) self.axes.w_zaxis.set_pane_color(colour) self.axes.xaxis.set_major_formatter(numformatter) self.axes.yaxis.set_major_formatter(timeFormatter) self.axes.zaxis.set_major_formatter(numformatter) self.axes.xaxis.set_minor_locator(AutoMinorLocator(10)) self.axes.yaxis.set_minor_locator(AutoMinorLocator(10)) self.axes.zaxis.set_minor_locator(AutoMinorLocator(10)) self.axes.set_xlim(self.settings.start, self.settings.stop) now = time.time() self.axes.set_ylim(utc_to_mpl(now), utc_to_mpl(now - 10)) self.axes.set_zlim(-50, 0) self.bar = self.figure.add_subplot(gs[1]) norm = Normalize(vmin=-50, vmax=0) self.barBase = ColorbarBase(self.bar, norm=norm, cmap=cm.get_cmap(self.settings.colourMap)) def scale_plot(self, force=False): if self.extent is not None and self.plot is not None: if self.settings.autoF or force: self.axes.set_xlim(self.extent.get_f()) if self.settings.autoL or force: self.axes.set_zlim(self.extent.get_l()) self.plot.set_clim(self.extent.get_l()) self.barBase.set_clim(self.extent.get_l()) try: self.barBase.draw_all() except: pass if self.settings.autoT or force: self.axes.set_ylim(self.extent.get_t()) def draw_measure(self, *args): pass def hide_measure(self): pass def redraw_plot(self): if self.figure is not None: post_event(self.notify, EventThread(Event.DRAW)) def get_axes(self): return self.axes def get_axes_bar(self): return self.barBase.ax def get_plot_thread(self): return self.threadPlot def set_title(self, title): self.axes.set_title(title, fontsize='medium') def set_plot(self, spectrum, extent, annotate=False): self.extent = extent self.threadPlot = ThreadPlot(self, self.settings, self.axes, spectrum, self.extent, self.barBase, annotate) self.threadPlot.start() def clear_plots(self): children = self.axes.get_children() for child in children: if child.get_gid() is not None: if child.get_gid() == "plot_line" or child.get_gid() == "peak": child.remove() def set_grid(self, on): self.axes.grid(on) self.redraw_plot() def set_colourmap(self, colourMap): if self.plot is not None: self.plot.set_cmap(colourMap) self.barBase.set_cmap(colourMap) try: self.barBase.draw_all() except: pass def close(self): self.figure.clear() self.figure = None
class Spectrogram: def __init__(self, notify, figure, settings): self.notify = notify self.figure = figure self.settings = settings self.data = [[], [], []] self.index = 0 self.axes = None self.plot = None self.extent = None self.lineHalfFS = None self.lineHalfFE = None self.lineObwFS = None self.lineObwFE = None self.labelHalfFS = None self.labelHalfFE = None self.labelObwFS = None self.labelObwFE = None self.threadPlot = None self.setup_plot() self.set_grid(self.settings.grid) def setup_plot(self): gs = GridSpec(1, 2, width_ratios=[9.5, 0.5]) self.axes = self.figure.add_subplot(gs[0], axisbg=self.settings.background) self.axes.set_xlabel("Frequency (MHz)") self.axes.set_ylabel('Time') numFormatter = ScalarFormatter(useOffset=False) timeFormatter = DateFormatter("%H:%M:%S") self.axes.xaxis.set_major_formatter(numFormatter) self.axes.yaxis.set_major_formatter(timeFormatter) self.axes.xaxis.set_minor_locator(AutoMinorLocator(10)) self.axes.yaxis.set_minor_locator(AutoMinorLocator(10)) self.axes.set_xlim(self.settings.start, self.settings.stop) now = time.time() self.axes.set_ylim(epoch_to_mpl(now), epoch_to_mpl(now - 10)) self.bar = self.figure.add_subplot(gs[1]) norm = Normalize(vmin=-50, vmax=0) self.barBase = ColorbarBase(self.bar, norm=norm, cmap=cm.get_cmap(self.settings.colourMap)) self.barBase.set_label('Level (dB)') self.setup_measure() def setup_measure(self): dashesHalf = [1, 5, 5, 5, 5, 5] self.lineHalfFS = Line2D([0, 0], [0, 0], dashes=dashesHalf, color='purple') self.lineHalfFE = Line2D([0, 0], [0, 0], dashes=dashesHalf, color='purple') self.lineObwFS = Line2D([0, 0], [0, 0], dashes=dashesHalf, color='#996600') self.lineObwFE = Line2D([0, 0], [0, 0], dashes=dashesHalf, color='#996600') if matplotlib.__version__ >= '1.3': effect = patheffects.withStroke(linewidth=3, foreground="w", alpha=0.75) self.lineHalfFS.set_path_effects([effect]) self.lineHalfFE.set_path_effects([effect]) self.lineObwFS.set_path_effects([effect]) self.lineObwFE.set_path_effects([effect]) self.axes.add_line(self.lineHalfFS) self.axes.add_line(self.lineHalfFE) self.axes.add_line(self.lineObwFS) self.axes.add_line(self.lineObwFE) box = dict(boxstyle='round', fc='white', ec='purple') self.labelHalfFS = Text(0, 0, '-3dB', fontsize='x-small', ha="center", va="top", bbox=box, color='purple') self.labelHalfFE = Text(0, 0, '-3dB', fontsize='x-small', ha="center", va="top", bbox=box, color='purple') box['ec'] = '#996600' self.labelObwFS = Text(0, 0, 'OBW', fontsize='x-small', ha="center", va="top", bbox=box, color='#996600') self.labelObwFE = Text(0, 0, 'OBW', fontsize='x-small', ha="center", va="top", bbox=box, color='#996600') self.axes.add_artist(self.labelHalfFS) self.axes.add_artist(self.labelHalfFE) self.axes.add_artist(self.labelObwFS) self.axes.add_artist(self.labelObwFE) self.hide_measure() def draw_vline(self, line, label, x): yLim = self.axes.get_ylim() xLim = self.axes.get_xlim() if xLim[0] < x < xLim[1]: line.set_visible(True) line.set_xdata([x, x]) line.set_ydata([yLim[0], yLim[1]]) self.axes.draw_artist(line) label.set_visible(True) label.set_position((x, yLim[1])) self.axes.draw_artist(label) def draw_measure(self, background, measure, show): if self.axes._cachedRenderer is None: return self.hide_measure() canvas = self.axes.get_figure().canvas canvas.restore_region(background) if show[Measure.HBW]: xStart, xEnd, _y = measure.get_hpw() self.draw_vline(self.lineHalfFS, self.labelHalfFS, xStart) self.draw_vline(self.lineHalfFE, self.labelHalfFE, xEnd) if show[Measure.OBW]: xStart, xEnd, _y = measure.get_obw() self.draw_vline(self.lineObwFS, self.labelObwFS, xStart) self.draw_vline(self.lineObwFE, self.labelObwFE, xEnd) canvas.blit(self.axes.bbox) def hide_measure(self): self.labelHalfFS.set_visible(False) self.labelHalfFE.set_visible(False) self.labelObwFS.set_visible(False) self.labelObwFE.set_visible(False) def scale_plot(self, force=False): if self.figure is not None and self.plot is not None: extent = self.plot.get_extent() if self.settings.autoF or force: if extent[0] == extent[1]: extent[1] += 1 self.axes.set_xlim(extent[0], extent[1]) if self.settings.autoL or force: vmin, vmax = self.plot.get_clim() self.barBase.set_clim(vmin, vmax) try: self.barBase.draw_all() except: pass if self.settings.autoT or force: self.axes.set_ylim(extent[2], extent[3]) def redraw_plot(self): if self.figure is not None: post_event(self.notify, EventThreadStatus(Event.DRAW)) def get_axes(self): return self.axes def get_plot_thread(self): return self.threadPlot def set_title(self, title): self.axes.set_title(title) def set_plot(self, data, extent, annotate=False): self.extent = extent self.threadPlot = ThreadPlot(self, self.axes, data, self.extent, self.settings.retainMax, self.settings.colourMap, self.settings.autoL, self.barBase, annotate) self.threadPlot.start() def clear_plots(self): children = self.axes.get_children() for child in children: if child.get_gid() is not None: if child.get_gid() == "plot" or child.get_gid() == "peak": child.remove() def set_grid(self, on): if on: self.axes.grid(True, color='w') else: self.axes.grid(False) self.redraw_plot() def set_colourmap(self, colourMap): if self.plot is not None: self.plot.set_cmap(colourMap) self.barBase.set_cmap(colourMap) try: self.barBase.draw_all() except: pass def close(self): self.figure.clear() self.figure = None
class Plotter(): def __init__(self, notify, figure, settings): self.notify = notify self.figure = figure self.settings = settings self.average = settings.average self.axes = None self.bar = None self.threadPlot = None self.extent = None self.lineMinP = None self.lineMaxP = None self.lineAvgP = None self.lineGMP = None self.lineHalfP = None self.lineHalfFS = None self.lineHalfFE = None self.lineObwP = None self.lineObwFS = None self.lineObwFE = None self.labelMinP = None self.labelMaxP = None self.labelAvgP = None self.labelGMP = None self.labelHalfP = None self.labelHalfFS = None self.labelHalfFE = None self.labelObwFS = None self.labelObwFE = None self.setup_plot() self.set_grid(self.settings.grid) def setup_plot(self): formatter = ScalarFormatter(useOffset=False) gs = GridSpec(1, 2, width_ratios=[9.5, 0.5]) self.axes = self.figure.add_subplot(gs[0], axisbg=self.settings.background) self.axes.set_xlabel("Frequency (MHz)") self.axes.set_ylabel('Level (dB)') self.axes.xaxis.set_major_formatter(formatter) self.axes.yaxis.set_major_formatter(formatter) self.axes.xaxis.set_minor_locator(AutoMinorLocator(10)) self.axes.yaxis.set_minor_locator(AutoMinorLocator(10)) self.axes.set_xlim(self.settings.start, self.settings.stop) self.axes.set_ylim(-50, 0) self.bar = self.figure.add_subplot(gs[1]) norm = Normalize(vmin=-50, vmax=0) self.barBase = ColorbarBase(self.bar, norm=norm, cmap=cm.get_cmap(self.settings.colourMap)) self.barBase.set_label('Level (dB)') self.setup_measure() def setup_measure(self): dashesAvg = [4, 5, 1, 5, 1, 5] dashesGM = [5, 5, 5, 5, 1, 5, 1, 5] dashesHalf = [1, 5, 5, 5, 5, 5] self.lineMinP = Line2D([0, 0], [0, 0], linestyle='--', color='black') self.lineMaxP = Line2D([0, 0], [0, 0], linestyle='-.', color='black') self.lineAvgP = Line2D([0, 0], [0, 0], dashes=dashesAvg, color='magenta') self.lineGMP = Line2D([0, 0], [0, 0], dashes=dashesGM, color='green') self.lineHalfP = Line2D([0, 0], [0, 0], dashes=dashesHalf, color='purple') self.lineHalfFS = Line2D([0, 0], [0, 0], dashes=dashesHalf, color='purple') self.lineHalfFE = Line2D([0, 0], [0, 0], dashes=dashesHalf, color='purple') self.lineObwP = Line2D([0, 0], [0, 0], dashes=dashesHalf, color='#996600') self.lineObwFS = Line2D([0, 0], [0, 0], dashes=dashesHalf, color='#996600') self.lineObwFE = Line2D([0, 0], [0, 0], dashes=dashesHalf, color='#996600') if matplotlib.__version__ >= '1.3': effect = patheffects.withStroke(linewidth=3, foreground="w", alpha=0.75) self.lineMinP.set_path_effects([effect]) self.lineMaxP.set_path_effects([effect]) self.lineAvgP.set_path_effects([effect]) self.lineGMP.set_path_effects([effect]) self.lineHalfP.set_path_effects([effect]) self.lineHalfFS.set_path_effects([effect]) self.lineHalfFE.set_path_effects([effect]) self.lineObwP.set_path_effects([effect]) self.lineObwFS.set_path_effects([effect]) self.lineObwFE.set_path_effects([effect]) self.axes.add_line(self.lineMinP) self.axes.add_line(self.lineMaxP) self.axes.add_line(self.lineAvgP) self.axes.add_line(self.lineGMP) self.axes.add_line(self.lineHalfP) self.axes.add_line(self.lineHalfFS) self.axes.add_line(self.lineHalfFE) self.axes.add_line(self.lineObwP) self.axes.add_line(self.lineObwFS) self.axes.add_line(self.lineObwFE) box = dict(boxstyle='round', fc='white', ec='black') self.labelMinP = Text(0, 0, 'Min', fontsize='x-small', ha="right", va="bottom", bbox=box, color='black') self.labelMaxP = Text(0, 0, 'Max', fontsize='x-small', ha="right", va="top", bbox=box, color='black') box['ec'] = 'magenta' self.labelAvgP = Text(0, 0, 'Mean', fontsize='x-small', ha="right", va="center", bbox=box, color='magenta') box['ec'] = 'green' self.labelGMP = Text(0, 0, 'GMean', fontsize='x-small', ha="right", va="center", bbox=box, color='green') box['ec'] = 'purple' self.labelHalfP = Text(0, 0, '-3dB', fontsize='x-small', ha="right", va="center", bbox=box, color='purple') self.labelHalfFS = Text(0, 0, '-3dB', fontsize='x-small', ha="center", va="top", bbox=box, color='purple') self.labelHalfFE = Text(0, 0, '-3dB', fontsize='x-small', ha="center", va="top", bbox=box, color='purple') box['ec'] = '#996600' self.labelObwP = Text(0, 0, 'OBW', fontsize='x-small', ha="right", va="center", bbox=box, color='#996600') self.labelObwFS = Text(0, 0, 'OBW', fontsize='x-small', ha="center", va="top", bbox=box, color='#996600') self.labelObwFE = Text(0, 0, 'OBW', fontsize='x-small', ha="center", va="top", bbox=box, color='#996600') self.axes.add_artist(self.labelMinP) self.axes.add_artist(self.labelMaxP) self.axes.add_artist(self.labelAvgP) self.axes.add_artist(self.labelGMP) self.axes.add_artist(self.labelHalfP) self.axes.add_artist(self.labelHalfFS) self.axes.add_artist(self.labelHalfFE) self.axes.add_artist(self.labelObwP) self.axes.add_artist(self.labelObwFS) self.axes.add_artist(self.labelObwFE) self.hide_measure() def draw_hline(self, line, label, y): xLim = self.axes.get_xlim() yLim = self.axes.get_ylim() if yLim[0] < y < yLim[1]: line.set_visible(True) line.set_xdata([xLim[0], xLim[1]]) line.set_ydata([y, y]) self.axes.draw_artist(line) label.set_visible(True) label.set_position((xLim[1], y)) self.axes.draw_artist(label) def draw_vline(self, line, label, x): yLim = self.axes.get_ylim() xLim = self.axes.get_xlim() if xLim[0] < x < xLim[1]: line.set_visible(True) line.set_xdata([x, x]) line.set_ydata([yLim[0], yLim[1]]) self.axes.draw_artist(line) label.set_visible(True) label.set_position((x, yLim[1])) self.axes.draw_artist(label) def draw_measure(self, background, measure, show): if self.axes._cachedRenderer is None: return self.hide_measure() canvas = self.axes.get_figure().canvas canvas.restore_region(background) if show[Measure.MIN]: y = measure.get_min_p()[1] self.draw_hline(self.lineMinP, self.labelMinP, y) if show[Measure.MAX]: y = measure.get_max_p()[1] self.draw_hline(self.lineMaxP, self.labelMaxP, y) if show[Measure.AVG]: y = measure.get_avg_p() self.draw_hline(self.lineAvgP, self.labelAvgP, y) if show[Measure.GMEAN]: y = measure.get_gmean_p() self.draw_hline(self.lineGMP, self.labelGMP, y) if show[Measure.HBW]: xStart, xEnd, y = measure.get_hpw() self.draw_hline(self.lineHalfP, self.labelHalfP, y) self.draw_vline(self.lineHalfFS, self.labelHalfFS, xStart) self.draw_vline(self.lineHalfFE, self.labelHalfFE, xEnd) if show[Measure.OBW]: xStart, xEnd, y = measure.get_obw() self.draw_hline(self.lineObwP, self.labelObwP, y) self.draw_vline(self.lineObwFS, self.labelObwFS, xStart) self.draw_vline(self.lineObwFE, self.labelObwFE, xEnd) canvas.blit(self.axes.bbox) def hide_measure(self): self.lineMinP.set_visible(False) self.lineMaxP.set_visible(False) self.lineAvgP.set_visible(False) self.lineGMP.set_visible(False) self.lineHalfP.set_visible(False) self.lineHalfFS.set_visible(False) self.lineHalfFE.set_visible(False) self.lineObwP.set_visible(False) self.lineObwFS.set_visible(False) self.lineObwFE.set_visible(False) self.labelMinP.set_visible(False) self.labelMaxP.set_visible(False) self.labelAvgP.set_visible(False) self.labelGMP.set_visible(False) self.labelHalfP.set_visible(False) self.labelHalfFS.set_visible(False) self.labelHalfFE.set_visible(False) self.labelObwP.set_visible(False) self.labelObwFS.set_visible(False) self.labelObwFE.set_visible(False) def scale_plot(self, force=False): if self.extent is not None: if self.settings.autoF or force: self.axes.set_xlim(self.extent.get_f()) if self.settings.autoL or force: self.axes.set_ylim(self.extent.get_l()) self.barBase.set_clim(self.extent.get_l()) norm = Normalize(vmin=self.extent.get_l()[0], vmax=self.extent.get_l()[1]) for collection in self.axes.collections: collection.set_norm(norm) try: self.barBase.draw_all() except: pass def redraw_plot(self): if self.figure is not None: post_event(self.notify, EventThreadStatus(Event.DRAW)) def get_axes(self): return self.axes def get_plot_thread(self): return self.threadPlot def set_title(self, title): self.axes.set_title(title) def set_plot(self, spectrum, extent, annotate=False): self.extent = extent self.threadPlot = ThreadPlot(self, self.axes, spectrum, self.extent, self.settings.colourMap, self.settings.autoL, self.settings.lineWidth, self.barBase, self.settings.fadeScans, annotate, self.settings.average) self.threadPlot.start() def clear_plots(self): children = self.axes.get_children() for child in children: if child.get_gid() is not None: if child.get_gid() == "plot" or child.get_gid() == "peak": child.remove() def set_grid(self, on): self.axes.grid(on) self.redraw_plot() def set_colourmap(self, colourMap): for collection in self.axes.collections: collection.set_cmap(colourMap) self.barBase.set_cmap(colourMap) try: self.barBase.draw_all() except: pass def close(self): self.figure.clear() self.figure = None
def draw( self ): PlotBase.draw( self ) self.x_formatter_cb( self.ax ) if self.gdata.isEmpty(): return None tmp_x = []; tmp_y = [] # Evaluate the bar width width = float( self.width ) offset = 0. if self.gdata.key_type == 'time': width = width / 86400.0 start_plot = 0 end_plot = 0 if "starttime" in self.prefs and "endtime" in self.prefs: start_plot = date2num( datetime.datetime.fromtimestamp( to_timestamp( self.prefs['starttime'] ) ) ) end_plot = date2num( datetime.datetime.fromtimestamp( to_timestamp( self.prefs['endtime'] ) ) ) labels = self.gdata.getLabels() nKeys = self.gdata.getNumberOfKeys() tmp_b = [] tmp_x = [] tmp_y = [] self.bars = [] labels = self.gdata.getLabels() nLabel = 0 labelNames = [] colors = [] xmin = None xmax = None for label, num in labels: labelNames.append( label ) for key, value in self.gdata.getPlotNumData( label ): if xmin is None or xmin > ( key + offset ): xmin = key + offset if xmax is None or xmax < ( key + offset ): xmax = key + offset if value is not None: colors.append( self.getQualityColor( value ) ) tmp_x.append( key + offset ) tmp_y.append( 1. ) tmp_b.append( float( nLabel ) ) nLabel += 1 self.bars += self.ax.bar( tmp_x, tmp_y, bottom = tmp_b, width = width, color = colors ) dpi = self.prefs.get( 'dpi', 100 ) setp( self.bars, linewidth = pixelToPoint( 0.5, dpi ), edgecolor = '#AAAAAA' ) #pivots = keys #for idx in range(len(pivots)): # self.coords[ pivots[idx] ] = self.bars[idx] ymax = float( nLabel ) self.ax.set_xlim( xmin = 0., xmax = xmax + width + offset ) self.ax.set_ylim( ymin = 0., ymax = ymax ) if self.gdata.key_type == 'time': if start_plot and end_plot: self.ax.set_xlim( xmin = start_plot, xmax = end_plot ) else: self.ax.set_xlim( xmin = min( tmp_x ), xmax = max( tmp_x ) ) self.ax.set_yticks( [ i + 0.5 for i in range( nLabel ) ] ) self.ax.set_yticklabels( labelNames ) setp( self.ax.get_xticklines(), markersize = 0. ) setp( self.ax.get_yticklines(), markersize = 0. ) cax, kw = make_axes( self.ax, orientation = 'vertical', fraction = 0.07 ) cb = ColorbarBase( cax, cmap = cm.RdYlGn, norm = self.norms ) cb.draw_all()
class Spectrogram(object): def __init__(self, notify, figure, settings): self.notify = notify self.figure = figure self.settings = settings self.data = [[], [], []] self.axes = None self.plot = None self.extent = None self.bar = None self.barBase = None self.lines = {} self.labels = {} self.overflowLabels = {} self.overflow = {'left': [], 'right': []} self.threadPlot = None self.__setup_plot() self.set_grid(self.settings.grid) def __setup_plot(self): gs = GridSpec(1, 2, width_ratios=[9.5, 0.5]) self.axes = self.figure.add_subplot(gs[0], facecolor=self.settings.background) self.axes.set_xlabel("Frequency (MHz)") self.axes.set_ylabel('Time') numFormatter = ScalarFormatter(useOffset=False) timeFormatter = DateFormatter("%H:%M:%S") self.axes.xaxis.set_major_formatter(numFormatter) self.axes.yaxis.set_major_formatter(timeFormatter) self.axes.xaxis.set_minor_locator(AutoMinorLocator(10)) self.axes.yaxis.set_minor_locator(AutoMinorLocator(10)) self.axes.set_xlim(self.settings.start, self.settings.stop) now = time.time() self.axes.set_ylim(utc_to_mpl(now), utc_to_mpl(now - 10)) self.bar = self.figure.add_subplot(gs[1]) norm = Normalize(vmin=-50, vmax=0) self.barBase = ColorbarBase(self.bar, norm=norm, cmap=cm.get_cmap(self.settings.colourMap)) self.__setup_measure() self.__setup_overflow() self.hide_measure() def __setup_measure(self): dashesHalf = [1, 5, 5, 5, 5, 5] self.lines[Markers.HFS] = Line2D([0, 0], [0, 0], dashes=dashesHalf, color='purple') self.lines[Markers.HFE] = Line2D([0, 0], [0, 0], dashes=dashesHalf, color='purple') self.lines[Markers.OFS] = Line2D([0, 0], [0, 0], dashes=dashesHalf, color='#996600') self.lines[Markers.OFE] = Line2D([0, 0], [0, 0], dashes=dashesHalf, color='#996600') if matplotlib.__version__ >= '1.3': effect = patheffects.withStroke(linewidth=3, foreground="w", alpha=0.75) self.lines[Markers.HFS].set_path_effects([effect]) self.lines[Markers.HFE].set_path_effects([effect]) self.lines[Markers.OFS].set_path_effects([effect]) self.lines[Markers.OFE].set_path_effects([effect]) for line in self.lines.itervalues(): self.axes.add_line(line) bbox = self.axes.bbox box = dict(boxstyle='round', fc='white', ec='purple', clip_box=bbox) self.labels[Markers.HFS] = Text(0, 0, '-3dB', fontsize='xx-small', ha="center", va="top", bbox=box, color='purple') self.labels[Markers.HFE] = Text(0, 0, '-3dB', fontsize='xx-small', ha="center", va="top", bbox=box, color='purple') box['ec'] = '#996600' self.labels[Markers.OFS] = Text(0, 0, 'OBW', fontsize='xx-small', ha="center", va="top", bbox=box, color='#996600') self.labels[Markers.OFE] = Text(0, 0, 'OBW', fontsize='xx-small', ha="center", va="top", bbox=box, color='#996600') for label in self.labels.itervalues(): self.axes.add_artist(label) def __setup_overflow(self): bbox = self.axes.bbox box = dict(boxstyle='round', fc='white', ec='black', alpha=0.5, clip_box=bbox) self.overflowLabels['left'] = Text(0, 0.9, '', fontsize='xx-small', ha="left", va="top", bbox=box, transform=self.axes.transAxes, alpha=0.5) self.overflowLabels['right'] = Text(1, 0.9, '', fontsize='xx-small', ha="right", va="top", bbox=box, transform=self.axes.transAxes, alpha=0.5) for label in self.overflowLabels.itervalues(): self.axes.add_artist(label) def __clear_overflow(self): for label in self.overflowLabels: self.overflow[label] = [] def __draw_vline(self, marker, x): line = self.lines[marker] label = self.labels[marker] yLim = self.axes.get_ylim() xLim = self.axes.get_xlim() if xLim[0] < x < xLim[1]: line.set_visible(True) line.set_xdata([x, x]) line.set_ydata([yLim[0], yLim[1]]) self.axes.draw_artist(line) label.set_visible(True) label.set_position((x, yLim[1])) self.axes.draw_artist(label) elif x is not None and x < xLim[0]: self.overflow['left'].append(marker) elif x is not None and x > xLim[1]: self.overflow['right'].append(marker) def __draw_overflow(self): for pos, overflow in self.overflow.iteritems(): if len(overflow) > 0: text = '' for measure in overflow: if len(text) > 0: text += '\n' text += self.labels[measure].get_text() label = self.overflowLabels[pos] if pos == 'left': textMath = '$\\blacktriangleleft$\n' + text elif pos == 'right': textMath = '$\\blacktriangleright$\n' + text label.set_text(textMath) label.set_visible(True) self.axes.draw_artist(label) def draw_measure(self, measure, show): if self.axes.get_renderer_cache() is None: return self.hide_measure() self.__clear_overflow() if show[Measure.HBW]: xStart, xEnd, _y = measure.get_hpw() self.__draw_vline(Markers.HFS, xStart) self.__draw_vline(Markers.HFE, xEnd) if show[Measure.OBW]: xStart, xEnd, _y = measure.get_obw() self.__draw_vline(Markers.OFS, xStart) self.__draw_vline(Markers.OFE, xEnd) self.__draw_overflow() def hide_measure(self): for line in self.lines.itervalues(): line.set_visible(False) for label in self.labels.itervalues(): label.set_visible(False) for label in self.overflowLabels.itervalues(): label.set_visible(False) def scale_plot(self, force=False): if self.figure is not None and self.plot is not None: extent = self.plot.get_extent() if self.settings.autoF or force: if extent[0] == extent[1]: extent[1] += 1 self.axes.set_xlim(extent[0], extent[1]) if self.settings.autoL or force: vmin, vmax = self.plot.get_clim() self.barBase.set_clim(vmin, vmax) try: self.barBase.draw_all() except: pass if self.settings.autoT or force: self.axes.set_ylim(extent[2], extent[3]) def redraw_plot(self): if self.figure is not None: post_event(self.notify, EventThread(Event.DRAW)) def get_axes(self): return self.axes def get_axes_bar(self): return self.barBase.ax def get_bar(self): return self.barBase def get_plot_thread(self): return self.threadPlot def set_title(self, title): self.axes.set_title(title, fontsize='medium') def set_plot(self, spectrum, extent, annotate=False): self.extent = extent self.threadPlot = ThreadPlot(self, self.settings, self.axes, spectrum, self.extent, self.barBase, annotate) self.threadPlot.start() def clear_plots(self): children = self.axes.get_children() for child in children: if child.get_gid() is not None: if child.get_gid() in [ 'plot', 'peak', 'peakText', 'peakShadow', 'peakThres' ]: child.remove() def set_grid(self, on): if on: self.axes.grid(True, color='w') else: self.axes.grid(False) self.redraw_plot() def set_colourmap(self, colourMap): if self.plot is not None: self.plot.set_cmap(colourMap) self.barBase.set_cmap(colourMap) try: self.barBase.draw_all() except: pass def close(self): self.figure.clear() self.figure = None