def save_plot(plot, filename, width, height): plt_bounds = plot.outer_bounds plot.do_layout(force=True) gc = PlotGraphicsContext(plt_bounds, dpi=72) gc.render_component(plot) gc.save(filename) print "Plot saved to: ", filename
def OnMenuExportPNG(self, e=None): """ Saves plot container as png """ dlg = wx.FileDialog(self, "Export plot as PNG", self.config.GetWorkingDirectory("PNG"), "", "PDF file (*.png)|*.png", wx.FD_SAVE|wx.FD_OVERWRITE_PROMPT) if dlg.ShowModal() == wx.ID_OK: path = dlg.GetPath() if not path.endswith(".png"): path += ".png" self.config.SetWorkingDirectory(os.path.dirname(path), "PNG") container = self.PlotArea.mainplot.container # get inner_boundary p = container dpi=600 p.do_layout(force=True) gc = PlotGraphicsContext(tuple(p.outer_bounds), dpi=dpi) # temporarily turn off the backbuffer for offscreen rendering use_backbuffer = p.use_backbuffer p.use_backbuffer = False p.draw(gc) #gc.render_component(p) gc.save(path) p.use_backbuffer = use_backbuffer
def _SaveFlag_changed(self): DPI = 72 print '--------------Save Initiated------------------' #Main plot save code size = (self.IntensityData[:,:,self.intensityindex].shape[0]*4, self.IntensityData[:,:,self.intensityindex].shape[1]*4) path = os.getenv('PWD') filenamelist = [path, '/', 'MainPlot_WaveLen', str(self.Wavelength).replace('.','_'), '.png'] filename = ''.join(filenamelist) container = self.Main_img_plot temp = container.outer_bounds container.outer_bounds = list(size) container.do_layout(force=True) gc = PlotGraphicsContext(size, dpi=DPI) gc.render_component(container) gc.save(filename) container.outer_bounds = temp print "SAVED: ", filename #Spectra plot save code size = (1000,500) path = os.getenv('PWD') filenamelist = [path, '/', 'SpectraPlot_X', str(self.InspectorPosition[0]), '_Y', str(self.InspectorPosition[1]), '.png'] filename = ''.join(filenamelist) container = self.Spectraplot1 temp = container.outer_bounds container.outer_bounds = list(size) container.do_layout(force=True) gc = PlotGraphicsContext(size, dpi=DPI) gc.render_component(container) gc.save(filename) container.outer_bounds = temp print "SAVED: ", filename return
def _save_raster(self): ''' Saves an image of the component. ''' from chaco.api import PlotGraphicsContext gc = PlotGraphicsContext((int(self.component.outer_width), int(self.component.outer_height))) self.component.draw(gc, mode="normal") gc.save(self.filename) return
def save_plot(plot, filename, width, height): # http://docs.enthought.com/chaco/user_manual/how_do_i.html from chaco.api import PlotGraphicsContext plot.outer_bounds = [width, height] plot.do_layout(force=True) gc = PlotGraphicsContext((width, height), dpi=72) gc.render_component(plot) gc.save(filename)
def draw_plot(filename, size=(800, 600)): container = create_plot() container.outer_bounds = list(size) container.do_layout(force=True) gc = PlotGraphicsContext(size, dpi=DPI) gc.render_component(container) gc.save(filename) return
def test_scatter_1d_selection(self): self.scatterplot.index.metadata['selections'] = [ (arange(10) % 2 == 0), ] gc = PlotGraphicsContext(self.size) gc.render_component(self.scatterplot) actual = gc.bmp_array[:, :, :] self.assertFalse(alltrue(actual == 255))
def _save_plot(self, plot, filename, width=800, height=600, dpi=72): self.set_plot_title(plot, self.plot_title) original_outer_bounds = plot.outer_bounds plot.outer_bounds = [width, height] plot.do_layout(force=True) gc = PlotGraphicsContext((width, height), dpi=dpi) gc.render_component(plot) gc.save(filename) plot.outer_bounds = original_outer_bounds
def _save(self): # Create a graphics context of the right size win_size = self.plot.outer_bounds plot_gc = PlotGraphicsContext(win_size) # Have the plot component into it plot_gc.render_component(self.plot) # Save out to the user supplied filename plot_gc.save(self._save_file)
def test_scatter_1d(self): self.assertEqual(self.scatterplot.origin, 'bottom left') self.assertIsNone(self.scatterplot.x_mapper) self.assertEqual(self.scatterplot.y_mapper, self.scatterplot.index_mapper) gc = PlotGraphicsContext(self.size) gc.render_component(self.scatterplot) actual = gc.bmp_array[:, :, :] self.assertFalse(alltrue(actual == 255))
def test_scatter_1d_selection_mask_name(self): # select with a mask self.scatterplot.selection_metadata_name = 'highlight_masks' self.scatterplot.index.metadata['highlight_masks'] = [ (arange(10) % 2 == 0), ] gc = PlotGraphicsContext(self.size) gc.render_component(self.scatterplot) actual = gc.bmp_array[:, :, :] self.assertFalse(alltrue(actual == 255))
def save_raster(self, filename): """ Saves an image of a chaco component (e.g. 'Plot' or 'Container') to a raster file, such as .jpg or .png. The file type is terermined by the extension. """ from chaco.api import PlotGraphicsContext gc = PlotGraphicsContext(self.outer_bounds, dpi=72) self.draw(gc, mode="normal") #gc.render_component(self) gc.save(filename) return
def test_scatter_fast(self): """ Coverage test to check basic case works """ size = (50, 50) scatterplot = create_scatter_plot( data=[range(10), range(10)], border_visible=False, ) scatterplot.outer_bounds = list(size) gc = PlotGraphicsContext(size) gc.render_component(scatterplot) actual = gc.bmp_array[:, :, :] self.assertFalse(alltrue(actual == 255))
def save_plot(self, filename=None): ''' Saves an image of the component. ''' if filename is None: filename = 'plots/plot%05d.jpg' %(round(self.time/self.delay*self.time_factor)) d = os.path.dirname(filename) if d != '' and not os.path.exists(d): os.makedirs(os.path.dirname(filename)) gc = PlotGraphicsContext((int(self.component.outer_width), int(self.component.outer_height))) self.component.draw(gc, mode="normal") gc.save(filename)
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_scatter_circle(self): """ Coverage test to check circles work """ size = (50, 50) scatterplot = create_scatter_plot( data=[list(sm.xrange(10)), list(sm.xrange(10))], marker="circle", border_visible=False, ) scatterplot.outer_bounds = list(size) gc = PlotGraphicsContext(size) gc.render_component(scatterplot) actual = gc.bmp_array[:, :, :] self.assertFalse(alltrue(actual == 255))
def test_scatter_1d_horizontal_flipped(self): self.scatterplot.direction = 'flipped' self.scatterplot.orientation = 'h' self.assertEqual(self.scatterplot.origin, 'bottom right') self.assertEqual(self.scatterplot.x_mapper, self.scatterplot.index_mapper) self.assertIsNone(self.scatterplot.y_mapper) gc = PlotGraphicsContext(self.size) gc.render_component(self.scatterplot) actual = gc.bmp_array[:, :, :] self.assertFalse(alltrue(actual == 255))
def test_scatter_1d_custom(self): path = CompiledPath() path.move_to(-5, -5) path.line_to(5, 5) path.line_to(5, -5) path.line_to(-5, 5) path.line_to(-5, -5) self.scatterplot.marker = 'custom' self.scatterplot.custom_symbol = path gc = PlotGraphicsContext(self.size) gc.render_component(self.scatterplot) actual = gc.bmp_array[:, :, :] self.assertFalse(alltrue(actual == 255))
def test_scatter_slow(self): """ Coverage test to check multiple marker size works """ size = (50, 50) scatterplot = create_scatter_plot( data=[range(10), range(10)], marker="circle", border_visible=False, marker_size=range(1, 11), ) scatterplot.outer_bounds = list(size) gc = PlotGraphicsContext(size) gc.render_component(scatterplot) actual = gc.bmp_array[:, :, :] self.assertFalse(alltrue(actual == 255))
def on_savefig(self): """ Handles the user requesting that the image of the function is to be saved. """ import os dlg = FileDialog(parent=self.control, title='Save as image', default_directory=os.getcwd(), default_filename="", wildcard=WILDCARD, action='save as') if dlg.open() == OK: path = dlg.path print "Saving plot to", path, "..." try: """ Now we create a canvas of the appropriate size and ask it to render our component. (If we wanted to display this plot in a window, we would not need to create the graphics context ourselves; it would be created for us by the window.) """ # self._plot.bounds = [500,300] # self._plot.padding = 50 # plot_gc = PlotGraphicsContext(self._plot.outer_bounds) # plot_gc.render_component(self._plot) #self._plot_container.outer_bounds = list((800,600)) # plot_gc = PlotGraphicsContext((400,300), dpi=72.0) # plot_gc.render_component(self._plot_container) self.lplot.bounds = [500, 300] self.lplot.padding = 50 win_size = self.lplot.outer_bounds plot_gc = PlotGraphicsContext(win_size) # Have the plot component into it plot_gc.render_component(self.lplot) # Finally, we tell the graphics context # to save itself to disk as an image. plot_gc.save(path) except: print "Error saving!" raise print "Plot saved." return
def _png_export_fired(self): outfileName = self._getFilePath(defFileName=self.__DEF_FILE_NAME + '.png') if outfileName != None: from chaco.api import PlotGraphicsContext container = self.plot tempSize = copy.deepcopy( container.outer_bounds ) container.outer_bounds = list((self.width-1,self.height-1)) container.do_layout(force=True) # gc = PlotGraphicsContext((self.width,self.height), dpi=self.dpi) gc = PlotGraphicsContext((self.width-1,self.height-1)) gc.render_component(container) gc.save( outfileName ) container.outer_bounds = tempSize self.plot.request_redraw()
def draw_plot(filename, size=(800,600), num_plots=8, type='line', key=''): """ Save the plot, and generate the hover_data file. """ container = create_plot(num_plots, type) container.outer_bounds = list(size) container.do_layout(force=True) gc = PlotGraphicsContext(size, dpi=DPI) gc.render_component(container) if filename: gc.save(filename) script_filename = filename[:-4] + "_png_hover_data.js" else: script_filename = None plot = make_palettized_png_str(gc) script_data = write_hover_coords(container, key, script_filename) return (plot, script_data)
def _SaveFlag_changed(self): DPI = 72 #Main plot save code size = (1000,1000) path = os.getenv('PWD') filenamelist = [path, '/', 'SourcePlots_X', str(self.InspectorPosition[0]), '_Y', str(self.InspectorPosition[1]), '.png'] filename = ''.join(filenamelist) container = self.PrimaryPlotC temp = container.outer_bounds container.outer_bounds = list(size) container.do_layout(force=True) gc = PlotGraphicsContext(size, dpi=DPI) gc.render_component(container) gc.save(filename) container.outer_bounds = temp print "SAVED: ", filename
def setUp(self): self.index = ArrayDataSource(arange(10)) self.value = ArrayDataSource(arange(10)) self.color_data = ArrayDataSource(arange(10)) self.size_data = arange(10) self.index_range = DataRange1D() self.index_range.add(self.index) self.index_mapper = LinearMapper(range=self.index_range) self.value_range = DataRange1D() self.value_range.add(self.value) self.value_mapper = LinearMapper(range=self.value_range) self.color_range = DataRange1D() self.color_range.add(self.color_data) self.color_mapper = jet(self.color_range) self.scatterplot = ColormappedScatterPlot( index=self.index, value=self.value, index_mapper=self.index_mapper, value_mapper=self.value_mapper, color_data=self.color_data, marker_size=self.size_data, color_mapper=self.color_mapper, ) self.scatterplot.outer_bounds = [50, 50] self.gc = PlotGraphicsContext((50, 50))
def test_draw_border_simple(self): """ Borders should have the correct height and width. """ size = (5,5) container = Plot(padding=1, border_visible=True) container.outer_bounds = list(size) gc = PlotGraphicsContext(size) gc.render_component(container) desired = array(((255, 255, 255, 255, 255, 255), (255, 255, 255, 255, 255, 255), (255, 0, 0, 0, 255, 255), (255, 0, 255, 0, 255, 255), (255, 0, 0, 0, 255, 255), (255, 255, 255, 255, 255, 255))) actual = gc.bmp_array[:,:,0] self.assertRavelEqual(actual, desired)
def test_data_label_arrow_not_visible(self): # Regression test for https://github.com/enthought/chaco/issues/281 # Before the problem was fixed, this test (specifically, using # arrow_visible=False in the DataLabel constructor) would raise an # exception because of an undefined reference. size = (50, 50) plot = create_scatter_plot(data=[range(10), range(10)]) label = DataLabel(component=plot, data_point=(4, 4), marker_color="red", marker_size=3, label_position=(20, 50), label_style='bubble', label_text="Something interesting", label_format="at x=%(x).2f, y=%(y).2f", arrow_visible=False) plot.overlays.append(label) plot.outer_bounds = list(size) gc = PlotGraphicsContext(size) gc.render_component(plot)
def test_scatter_custom(self): """ Coverage test to check custom markers work """ # build path path = CompiledPath() path.move_to(-5, -5) path.line_to(5, 5) path.line_to(5, -5) path.line_to(-5, 5) path.line_to(-5, -5) size = (50, 50) scatterplot = create_scatter_plot( data=[range(10), range(10)], marker='custom', border_visible=False, ) scatterplot.custom_symbol = path scatterplot.outer_bounds = list(size) gc = PlotGraphicsContext(size) gc.render_component(scatterplot) actual = gc.bmp_array[:, :, :] self.assertFalse(alltrue(actual == 255))
def _export_fired(self): dialog = FileDialog(action="save as", wildcard='.dat') dialog.open() if dialog.return_code == OK: # prepare plot for saving width = 800 height = 600 self.plot.outer_bounds = [width, height] self.plot.do_layout(force=True) gc = PlotGraphicsContext((width, height), dpi=72) gc.render_component(self.plot) #save data,image,description timetag = "" #init empty str for later directory = dialog.directory t = self.acqtime #retrieve time from last aquisition timelist = [t.tm_year,t.tm_mon,t.tm_mday,t.tm_hour,t.tm_min,t.tm_sec] timelist = map(str,timelist) #convert every entry to str of timelist for i in range(len(timelist)): #if sec is eg "5" change to "05" if len(timelist[i]) == 1: timelist[i] = "0" + timelist[i] timetag = timetag.join(timelist) #make single str of all entries filename = timetag + dialog.filename # join timetag and inidivid. name print "target directory: " + directory gc.save(filename+".png") self.wvl = self.create_wavelength_for_plotting() x,y = self.icCryo.pos() #get cryo pos pos = "Cryo was at pos "+ str(x) + " x " + str(y)+" y" with open(filename+'.dat','wb') as f: f.write('#'+ str(pos)+ '\n'+ '#\n'+ '#\n'+ '#\n') #this is for the header np.savetxt(f, np.transpose([self.wvl,self.line]), delimiter='\t')
def test_errorbarplot(self): """ Coverage test to check basic case works """ size = (50, 50) x = np.array([1, 2]) y = np.array([5, 10]) errors = np.array([1, 2]) low = ArrayDataSource(y - errors) high = ArrayDataSource(y + errors) errorbar_plot = ErrorBarPlot( index=ArrayDataSource(x), values=ArrayDataSource(y), index_mapper=LinearMapper(range=DataRange1D(low=0, high=3)), value_mapper=LinearMapper(range=DataRange1D(low=0, high=15)), value_low=low, value_high=high, color='blue', line_width=3.0, ) errorbar_plot.outer_bounds = list(size) gc = PlotGraphicsContext(size) gc.render_component(errorbar_plot) actual = gc.bmp_array[:, :, :] self.assertFalse(alltrue(actual == 255))
def test_scatter_1d_line_style(self): self.scatterplot.line_style = 'dash' gc = PlotGraphicsContext(self.size) gc.render_component(self.scatterplot) actual = gc.bmp_array[:, :, :] self.assertFalse(alltrue(actual == 255))
def test_linescatter_1d_color(self): self.linescatterplot.color = 'orange' gc = PlotGraphicsContext(self.size) gc.render_component(self.linescatterplot) actual = gc.bmp_array[:, :, :] self.assertFalse(alltrue(actual == 255))
def save_renderer_result(renderer, filename): renderer.padding = 0 gc = PlotGraphicsContext(renderer.outer_bounds) with gc: gc.render_component(renderer) gc.save(filename)
def test_scatter_1d_circle(self): self.scatterplot.marker = 'circle' gc = PlotGraphicsContext(self.size) gc.render_component(self.scatterplot) actual = gc.bmp_array[:, :, :] self.assertFalse(alltrue(actual == 255))
def snapshot(self, params): gc = PlotGraphicsContext(self.conn_mat.outer_bounds, dpi=params.dpi) gc.render_component(self.conn_mat) gc.save(params.savefile)
def test_scatter_1d_rotated(self): self.scatterplot.text_rotate_angle = 45 gc = PlotGraphicsContext(self.size) gc.render_component(self.scatterplot) actual = gc.bmp_array[:, :, :] self.assertFalse(alltrue(actual == 255))
class TestColormappedScatterplot(unittest.TestCase): def setUp(self): self.index = ArrayDataSource(arange(10)) self.value = ArrayDataSource(arange(10)) self.color_data = ArrayDataSource(arange(10)) self.size_data = arange(10) self.index_range = DataRange1D() self.index_range.add(self.index) self.index_mapper = LinearMapper(range=self.index_range) self.value_range = DataRange1D() self.value_range.add(self.value) self.value_mapper = LinearMapper(range=self.value_range) self.color_range = DataRange1D() self.color_range.add(self.color_data) self.color_mapper = jet(self.color_range) self.scatterplot = ColormappedScatterPlot( index=self.index, value=self.value, index_mapper=self.index_mapper, value_mapper=self.value_mapper, color_data=self.color_data, marker_size=self.size_data, color_mapper=self.color_mapper, ) self.scatterplot.outer_bounds = [50, 50] self.gc = PlotGraphicsContext((50, 50)) def test_scatter_render(self): """ Coverage test to check basic case works """ self.gc.render_component(self.scatterplot) actual = self.gc.bmp_array[:, :, :] self.assertFalse(alltrue(actual == 255)) def test_scatter_circle(self): """ Coverage test to check circles work """ self.scatterplot.marker = 'circle' self.gc.render_component(self.scatterplot) actual = self.gc.bmp_array[:, :, :] self.assertFalse(alltrue(actual == 255)) @skip def test_scatter_custom(self): """ Coverage test to check custom markers work... XXX ...which apparently they currently don't. See #232. """ # build path path = CompiledPath() path.begin_path() path.move_to(-5, -5) path.line_to(-5, 5) path.line_to(5, 5) path.line_to(5, -5) path.line_to(-5, -5) self.scatterplot.marker = 'custom' self.scatterplot.custom_symbol = path self.gc.render_component(self.scatterplot) actual = self.gc.bmp_array[:, :, :] self.assertFalse(alltrue(actual == 255)) def test_colormap_updated(self): """ If colormapper updated then we need to redraw """ self.color_mapper.updated = True self.assertFalse(self.scatterplot.draw_valid)
def _save(self): win_size = self.plot.outer_bounds plot_gc = PlotGraphicsContext(win_size) #, dpi=300) plot_gc.render_component(self.plot) plot_gc.save("image_test.png")
def test_scatter_1d_thicker(self): self.scatterplot.line_width = 2.0 gc = PlotGraphicsContext(self.size) gc.render_component(self.scatterplot) actual = gc.bmp_array[:, :, :] self.assertFalse(alltrue(actual == 255))
def render_agg(): gc2 = PlotGraphicsContext((800, 600), dpi=DPI) gc2.render_component(container) gc2.save("/tmp/kiva_agg.png")
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), # dest_box_units="inch") # gc.render_component(plot)