def plotRU(self, rangeX=None, rangeY=None, save=False, filename=""): if save and filename == "": self.add_line("ERROR: I need a valid file name") return if save and filename.split('.')[-1] != "png": self.add_line("ERROR: File must end in .png") return if len(self.morseList) > 0: plotData = ArrayPlotData(x=self.Rlist, y=self.Ulist, morse=self.morseList, eigX=[self.Rlist[0], self.Rlist[-1]]) else: plotData = ArrayPlotData(x=self.Rlist, y=self.Ulist) for val in self.levelsToFind: if val < len(self.convergedValues): plotData.set_data( "eig" + str(val), [self.convergedValues[val], self.convergedValues[val]]) plot = Plot(plotData) if len(self.morseList) > 0: plot.plot(("x", "morse"), type="line", color="red") for val in self.levelsToFind: if val < len(self.convergedValues): plot.plot(("eigX", "eig" + str(val)), type="line", color="green") plot.plot(("x", "y"), type="line", color="blue") plot.plot(("x", "y"), type="scatter", marker_size=1.0, color="blue") # plot.index_axis.title = "Separation (r0)" if (self.scaled): plot.value_axis.title = "Potential (Eh * 2 * mu)" else: plot.value_axis.title = "Potential (Eh)" if len(self.plotRangeX) != 0: plot.x_axis.mapper.range.low = self.plotRangeX[0] plot.x_axis.mapper.range.high = self.plotRangeX[1] if len(self.plotRangeY) != 0: plot.y_axis.mapper.range.low = self.plotRangeY[0] plot.y_axis.mapper.range.high = self.plotRangeY[1] if not save: self.plot = plot else: plot.outer_bounds = [800, 600] plot.do_layout(force=True) gc = PlotGraphicsContext((800, 600), dpi=72) gc.render_component(plot) gc.save(filename)
def test_segment_plot(self): x = arange(10) y = arange(1, 11) data = ArrayPlotData(x=x, y=y) plot = Plot(data) plot.plot(('x', 'y'), "segment")[0] plot.do_layout((250, 250)) gc = PlotGraphicsContext((250, 250)) gc.render_component(plot) actual = gc.bmp_array[:, :, :] self.assertFalse(alltrue(actual == 255))
def plotRU(self,rangeX=None, rangeY=None, save=False, filename=""): if save and filename == "": self.add_line("ERROR: I need a valid file name") return if save and filename.split('.')[-1] != "png": self.add_line("ERROR: File must end in .png") return if len(self.morseList) > 0: plotData = ArrayPlotData(x=self.Rlist, y=self.Ulist, morse=self.morseList, eigX=[self.Rlist[0], self.Rlist[-1]]) else: plotData = ArrayPlotData(x=self.Rlist, y=self.Ulist) for val in self.levelsToFind: if val < len(self.convergedValues): plotData.set_data("eig"+str(val), [self.convergedValues[val], self.convergedValues[val]]) plot = Plot(plotData) if len(self.morseList) > 0: plot.plot(("x","morse"), type = "line", color = "red") for val in self.levelsToFind: if val < len(self.convergedValues): plot.plot(("eigX","eig"+str(val)), type="line", color="green") plot.plot(("x","y"), type = "line", color = "blue") plot.plot(("x","y"), type = "scatter", marker_size = 1.0, color = "blue") # plot.index_axis.title = "Separation (r0)" if (self.scaled): plot.value_axis.title = "Potential (Eh * 2 * mu)" else: plot.value_axis.title = "Potential (Eh)" if len(self.plotRangeX) != 0: plot.x_axis.mapper.range.low = self.plotRangeX[0] plot.x_axis.mapper.range.high = self.plotRangeX[1] if len(self.plotRangeY) != 0: plot.y_axis.mapper.range.low = self.plotRangeY[0] plot.y_axis.mapper.range.high = self.plotRangeY[1] if not save: self.plot = plot else: plot.outer_bounds = [800,600] plot.do_layout(force=True) gc = PlotGraphicsContext((800,600), dpi = 72) gc.render_component(plot) gc.save(filename)
def test_text_plot(self): x = arange(10) y = arange(1, 11) t = array(["one", "two", "three", "four", "five"]) data = ArrayPlotData(x=x, y=y, t=t) plot = Plot(data) plot.plot(('x', 'y', 't'), "text")[0] plot.do_layout((250, 250)) gc = PlotGraphicsContext((250, 250)) gc.render_component(plot) actual = gc.bmp_array[:, :, :] self.assertFalse(alltrue(actual == 255))
def test_segment_plot_color(self): x = arange(10) y = arange(1, 11) c = arange(2, 7) data = ArrayPlotData(x=x, y=y, c=c) plot = Plot(data) plot.plot(('x', 'y', 'c'), "cmap_segment", color_mapper=viridis)[0] plot.do_layout((250, 250)) gc = PlotGraphicsContext((250, 250)) gc.render_component(plot) actual = gc.bmp_array[:, :, :] self.assertFalse(alltrue(actual == 255))
class PoincarePlotFastMovieMakerWorker(object): """ special fast movie generation class which uses chaco library """ def __init__(self, pp_specs_manager): self.manager = pp_specs_manager self.pp_specs = self.manager.getMiniPoincarePlotSpecs() if len(self.pp_specs) == 0: return self.gc = None self.p0 = self.pp_specs[0] #set up specific values for fonts sizes based on products of a movie #height and arbitrary constants to give looking good picture if self.manager.movie_axis_font == None: self.axis_font = 'modern ' + str(nvl_and_positive( self.manager.movie_axis_font_size, self.manager.movie_height / 38)) else: self.axis_font = self.manager.movie_axis_font if self.manager.movie_title_font == None: self.title_font = 'modern ' + str(nvl_and_positive( self.manager.movie_title_font_size, self.manager.movie_height / 30)) else: self.title_font = self.manager.movie_title_font self.time_label_font = 'modern ' + str(nvl_and_positive( self.manager.movie_time_label_font_size, self.manager.movie_height / 35)) self.tick_font = nvl(self.manager.movie_tick_font, None) self.frame_pad = nvl_and_positive(self.manager.movie_frame_pad, 50) def initiate(self): if len(self.pp_specs) == 0: return False # only positive values are accepted x = self.p0.x_data[pl.where(self.p0.x_data > 0)] y = self.p0.y_data[pl.where(self.p0.y_data > 0)] x_min = pl.amin(x) x_max = pl.amax(x) y_min = pl.amin(y) y_max = pl.amax(y) value_min = x_min if x_min < y_min else y_min self.value_max = x_max if x_max > y_max else y_max self.pd = ArrayPlotData() self.pd.set_data("index", x) self.pd.set_data("value", y) index_ds = ArrayDataSource(x) value_ds = ArrayDataSource(y) # Create the plot self._plot = Plot(self.pd) axis_defaults = { #'axis_line_weight': 2, #'tick_weight': 2, #'tick_label_color': 'green', 'title_font': self.axis_font, } if self.tick_font: axis_defaults['tick_label_font'] = self.tick_font #a very important and weird trick; used to remove default ticks labels self._plot.x_axis = None self._plot.y_axis = None #end trick #add new x label and x's ticks labels x_axis = PlotAxis(orientation='bottom', title=nvl(self.manager.x_label, 'RR(n) [ms]'), mapper=self._plot.x_mapper, **axis_defaults) self._plot.overlays.append(x_axis) #add new y label and y's ticks labels y_axis = PlotAxis(orientation='left', title=nvl(self.manager.y_label, 'RR(n+1) [ms]'), mapper=self._plot.y_mapper, **axis_defaults) self._plot.overlays.append(y_axis) self._plot.index_range.add(index_ds) self._plot.value_range.add(value_ds) self._plot.index_mapper.stretch_data = False self._plot.value_mapper.stretch_data = False self._plot.value_range.set_bounds(value_min, self.value_max) self._plot.index_range.set_bounds(value_min, self.value_max) # Create the index and value mappers using the plot data ranges imapper = LinearMapper(range=self._plot.index_range) vmapper = LinearMapper(range=self._plot.value_range) color = "white" self.scatter = __PoincarePlotScatterPlot__( self.p0, self.manager, index=index_ds, value=value_ds, #color_data=color_ds, #color_mapper=color_mapper, #fill_alpha=0.4, color=color, index_mapper=imapper, value_mapper=vmapper, marker='circle', marker_size=self.manager.active_point_size, line_width=0 #outline_color='white' ) self._plot.add(self.scatter) #self._plot.plots['var_size_scatter'] = [self.scatter] # Tweak some of the plot properties _title = nvl(self.manager.movie_title, "Poincare plot") if len(_title) > 0: self._plot.title = _title self._plot.title_font = self.title_font self._plot.line_width = 0.5 self._plot.padding = self.frame_pad self._plot.do_layout(force=True) self._plot.outer_bounds = [self.manager.movie_width, self.manager.movie_height] self.gc = PlotGraphicsContext(self._plot.outer_bounds, dpi=self.manager.movie_dpi) self.gc.render_component(self._plot) self.gc.set_line_width(0) self.gc.save(self._get_filename(self.p0)) self.x_mean_old = None self.y_mean_old = None self._time_label_font = None return True def plot(self, idx): if len(self.pp_specs) == 0: return p = self.pp_specs[idx] p_old = None if idx == 0 else self.pp_specs[idx - 1] if idx > 0: self.gc.set_line_width(0) if not p_old == None and not p_old.mean_plus == None: r_points = pl.array([[p_old.mean_plus, p_old.mean_minus]]) r_points = self.scatter.map_screen(r_points) self.gc.set_fill_color((1.0, 1.0, 1.0, 1.0)) self.gc.draw_marker_at_points(r_points, self.manager.centroid_point_size, CIRCLE) __update_graphics_context__(self.gc, self.scatter, p, self.manager) r_points = pl.array([[p.mean_plus, p.mean_minus]]) r_points = self.scatter.map_screen(r_points) self.gc.set_fill_color(self.manager.centroid_color_as_tuple) self.gc.draw_marker_at_points(r_points, self.manager.centroid_point_size, CIRCLE) #self.gc.save_state() self._draw_time_text(self.gc, p) self.gc.save_state() if self.manager.movie_frame_step > 0 and idx > 0: # only frames module movie_frame_step are generated if not self.manager.movie_frame_step % idx == 0: return if self.manager.movie_identity_line: __draw_identity_line__(self.gc, self.value_max, self.scatter) self.gc.save(self._get_filename(p)) def _draw_time_text(self, gc, pp_spec): if self.manager.movie_create_time_label == False: return if pp_spec.level == 0: (H, M, S) = get_time_label_parts_for_miliseconds(0, hour_label=self.manager.movie_hour_label, minute_label=self.manager.movie_minute_label, second_label=self.manager.movie_second_label) else: (H, M, S) = get_time_label_parts_for_miliseconds( pp_spec.cum_inactive, hour_label=self.manager.movie_hour_label, minute_label=self.manager.movie_minute_label, second_label=self.manager.movie_second_label) if not self._time_label_font: self._time_label_font = str_to_font(None, None, self.time_label_font) gc.set_font(self._time_label_font) shift = 10 if self.manager.movie_time_label_in_line == True: if self.manager.movie_time_label_prefix: time_line = '%s %s %s %s' % (self.manager.movie_time_label_prefix, H, M, S) else: time_line = '%s %s %s' % (H, M, S) _, _, tw, th = gc.get_text_extent(time_line) x = self._plot.outer_bounds[0] / 2 - tw / 2 - self._plot.padding_left y = self._plot.outer_bounds[1] - self._plot.padding_top - th gc.set_fill_color((1.0, 1.0, 1.0, 1.0)) gc.rect(x, y, tw, th + shift) gc.draw_path() gc.set_fill_color((0.0, 0.0, 0.0, 1.0)) gc.show_text_at_point(time_line, x, y) else: for idx, time_e in enumerate([H, M, S]): _, _, tw, th = gc.get_text_extent(time_e) x = self._plot.outer_bounds[0] - tw - self._plot.padding_right y = self._plot.outer_bounds[1] / 2 - idx * (th + shift) gc.set_fill_color((1.0, 1.0, 1.0, 1.0)) #gc.rect(x, y, tw + shift, th) gc.rect(x, y, tw, th + shift) gc.draw_path() gc.set_fill_color((0.0, 0.0, 0.0, 1.0)) gc.show_text_at_point(time_e, x, y) return def _get_filename(self, pp_spec): if self.manager.movie_frame_filename_with_time: (H, M, S) = get_time_for_miliseconds(0 if pp_spec.level == 0 else pp_spec.cum_inactive) if pp_spec.frame_file.endswith(PNG_EXTENSION): filename = pp_spec.frame_file.rsplit(PNG_EXTENSION)[0] return '%s_%02d_%02d_%02d%s' % (filename, H, M, S, PNG_EXTENSION) return pp_spec.frame_file
from app_common.chaco.plot_io import save_plot_to_file from chaco.api import Plot, ArrayPlotData from chaco.api import PlotGraphicsContext from chaco.pdf_graphics_context import PdfPlotGraphicsContext from chaco.tools.api import SaveTool data = {"x": [1, 2, 3, 4, 5], "y": [1, 2, 3, 2, 1]} plot = Plot(ArrayPlotData(**data)) plot.plot(("x", "y"), type="scatter", color="blue") plot.padding = 20 plot.outer_bounds = (800, 600) plot.do_layout(force=True) gc = PlotGraphicsContext((800, 600), dpi=72) gc.render_component(plot, container_coords=(10, 10)) gc.save("test1.PNG") # save_plot_to_file(plot, "test1.PNG") # save_plot_to_file(plot, "test2.PNG") # gc = PlotGraphicsContext((800, 600), dpi=72) # plot.draw(gc) # gc.save("test1.JPG") # gc = PdfPlotGraphicsContext(filename="test1.PDF", # pagesize="letter", # dest_box=(0.5, 0.5, -0.5, -0.5),