def test_figure_io(): vpl.close() assert vpl.gcf(False) is None vpl.auto_figure(False) assert vpl.gcf() is None with pytest.raises(vpl.figures.figure_manager.NoFigureError): vpl.screenshot_fig() fig = vpl.figure() assert vpl.gcf() is None del fig vpl.auto_figure(True) fig = vpl.gcf() assert fig is not None assert fig is vpl.gcf() vpl.close() assert vpl.gcf(False) is None vpl.scf(fig) assert fig is vpl.gcf() vpl.close() fig = vpl.figure() assert fig is vpl.gcf() vpl.close()
def test_figure_io(self): vpl.close() self.assertIs(vpl.gcf(False), None) vpl.auto_figure(False) self.assertIs(vpl.gcf(), None) with self.assertRaises(vpl.figures.figure_manager.NoFigureError): vpl.screenshot_fig() fig = vpl.figure() self.assertIs(vpl.gcf(), None) del fig vpl.auto_figure(True) fig = vpl.gcf() self.assertTrue(fig is not None) self.assertIs(fig, vpl.gcf()) vpl.close() self.assertIs(vpl.gcf(False), None) vpl.scf(fig) self.assertIs(fig, vpl.gcf()) vpl.close() fig = vpl.figure() self.assertIs(fig, vpl.gcf()) vpl.close()
def create_figure(path_dict, figure_path, path_dict2=None, pair_mapping=None, transp_backg=False): assert ((path_dict2 is None) + (pair_mapping is None)) != 1, \ 'please specify all kwargs or none of them' if pair_mapping is not None: # for k in tqdm(pair_mapping): # mesh= Mesh.from_file(path_dict[k[0]]) # mesh2 = Mesh.from_file(path_dict2[k[1]]) for k, values in tqdm(pair_mapping.items()): mesh = Mesh.from_file(path_dict[k]) mesh = _add_normalizing_vector_point(mesh, 300, -300) fig = vpl.figure() fig.background_color = 'black' vpl.mesh_plot(mesh, color='pink', opacity=0.3) #make dendrite translucent for v in values: # add second, third,.. .stl to same plot mesh2 = Mesh.from_file(path_dict2[str(v)]) vpl.mesh_plot(mesh2) save_path = figure_path + str(k) + '.png' vpl.save_fig( save_path, magnification=5, off_screen=True, ) if transp_backg == True: #make black background transparent _transparent_background(save_path) fig.close() else: for k in tqdm(path_dict): # Read the STL using numpy-stl mesh = Mesh.from_file(path_dict[k]) if debug == True: mesh = _add_normalizing_vector_point(mesh, 300, -300) fig = vpl.figure() fig.background_color = 'black' vpl.mesh_plot(mesh) save_path = figure_path + str(k) + '.png' vpl.save_fig( save_path, magnification=5, off_screen=True, ) if transp_backg == True: #make black background transparent _transparent_background(save_path) fig.close()
def quick_show(self): from vtkplotlib import gcf, scf, figure old_gcf = gcf(False) fig = figure(name=repr(self)) fig += self fig.show() scf(old_gcf)
def quick_show(self): import vtkplotlib as vpl old_fig = vpl.gcf(create_new=False) fig = vpl.figure(repr(self)) plot = self.to_plot(fig) vpl.show(fig) vpl.scf(old_fig) return plot
def test_add_remove(self): fig = vpl.figure() plots = vpl.quick_test_plot(None) fig += plots fig.show(False) fig -= plots for i in plots: fig += i fig.update() time.sleep(.05) for i in plots: fig -= i fig.update() time.sleep(.05) vpl.close(fig)
def test_multi_figures(self): vpl.close() vpl.auto_figure(False) plot = vpl.plot(np.random.uniform(-10, 10, (10, 3)), join_ends=True) figs = [] for i in range(1, 4): fig = vpl.figure("figure {}".format(i)) fig += plot vpl.view(camera_direction=np.random.uniform(-1, 1, 3), fig=fig) vpl.reset_camera(fig) fig.show(False) figs.append(fig) fig.show() vpl.auto_figure(True)
def test_trim_image(): fig = vpl.figure() # Shouldn't do anything if the figure is empty. assert vpl.screenshot_fig(trim_pad_width=.05).shape[:2] == fig.render_size vpl.quick_test_plot() arr = vpl.screenshot_fig() vpl.close() trimmed = vpl.image_io.trim_image(arr, fig.background_color, 10) background_color = np.asarray(fig.background_color) * 255 # Check that no non-background coloured pixels have been lost. assert (arr != background_color).any(-1).sum() \ == (trimmed != background_color).any(-1).sum() return trimmed
def test_save(self): plots = vpl.scatter(np.random.uniform(-10, 10, (30, 3))) # I can't get python2 to cooperate with unicode here. # The os functions just don't like them. if sys.version[0] == "3": path = Path.cwd() / u"ҢघԝઌƔࢳܢˀા\\Հએࡓ\u061cཪЈतயଯ\u0886.png" try: os.mkdir(str(path.parent)) vpl.save_fig(path) self.assertTrue(path.exists()) os.remove(str(path)) finally: if path.parent.exists(): os.rmdir(str(path.parent)) else: path = Path.cwd() / "image.png" vpl.save_fig(path) os.remove(str(path)) array = vpl.screenshot_fig(2) self.assertEqual(array.shape, tuple(i * 2 for i in vpl.gcf().render_size) + (3, )) plt.imshow(array) plt.show() shape = tuple(i * j for (i, j) in zip(vpl.gcf().render_size, (2, 3))) vpl.screenshot_fig(pixels=shape).shape # The following will fail depending on VTK version # self.assertEqual(vpl.screenshot_fig(pixels=shape).shape, # shape[::-1] + (3,)) vpl.close() fig = vpl.figure() for plot in plots: fig += plot vpl.show()
# @position.setter # def position(self, position): # if self.use_pixels: # self._position = position # else: # self._position = tuple(int(i * j) for (i, j) in zip(position, self.fig.render_size)) @property def text(self): return self.actor.GetInput() @text.setter def text(self, text_str): if not isinstance(text_str, str): text_str = str(text_str) self.actor.SetInput(text_str) def resize_event_cb(*args): # print(args) self.actor.SetPosition(*(i // 2 for i in fig.render_size)) if __name__ == "__main__": import vtkplotlib as vpl fig = vpl.figure() fig.renWin.AddObserver(vtk.vtkCommand.ModifiedEvent, resize_event_cb) self = vpl.text("eggs", (100, 200)) vpl.show()
@property def window_name(self): if hasattr(self, "_renWin"): return self.renWin.GetWindowName() return self._window_name @window_name.setter def window_name(self, window_name): if hasattr(self, "_renWin"): self.renWin.SetWindowName(window_name) self._window_name = window_name if __name__ == "__main__": Figure._abc_assert_no_abstract_methods() import vtkplotlib as vpl self = vpl.figure("a normal vtk figure") # vpl.plot(np.random.uniform(-10, 10, (20, 3))) direction = np.array([1, 0, 0]) vpl.quiver(np.array([0, 0, 0]), direction) vpl.view(camera_direction=direction) vpl.reset_camera() # vpl.save_fig(Path.home() / "img.jpg", 1080) self.show()
# -*- coding: utf-8 -*-
import vtkplotlib as vpl import numpy as np # You can create a figure explicitly using figure() fig = vpl.figure("Your Figure Title Here") # Creating a figure automatically sets it as the current working figure # You can get the current figure using gcf() vpl.gcf() is fig # Should be True # If a figure hadn't been explicitly created using figure() then gcf() # would have created one. If gcf() had also not been called here then # the plotting further down will have internally called gcf(). # A figure's properties can be edited directly fig.background_color = "dark green" fig.window_name = "A New Window Title" points = np.random.uniform(-10, 10, (2, 3)) # To add to a figure you can either: # 1) Let it automatically add to the whichever figure gcf() returns vpl.scatter(points[0], color="r") # 2) Explicitly give it a figure to add to vpl.scatter(points[1], radius=2, fig=fig) # 3) Or pass fig=None to prevent it being added then add it later arrow = vpl.arrow(points[0], points[1], color="g", fig=None) fig += arrow