def test_scatter(): points = np.random.uniform(-10, 10, (30, 3)) colors = vpl.colors.normalise(points) radii = np.abs(points[:, 0])**.5 vpl.scatter(points, color=colors, radius=radii, use_cursors=False)[0] self = vpl.scatter(points, color=colors, radius=radii, use_cursors=True)[0] self.point += np.array([10, 0, 0])
def test_annotate(): point = np.array([1, 2, 3]) vpl.scatter(point) arrow, self = vpl.annotate(point, point, np.array([0, 0, 1]), text_color="green", arrow_color="purple")
def test_doc_10(self): import vtkplotlib as vpl import numpy as np # Create a ball at a point in space. point = np.array([1, 2, 3]) vpl.scatter(point) vpl.annotate(point, "This ball is at {}".format(point), np.array([0, 0, 1]))
def test_annotate(): point = np.array([1, 2, 3]) vpl.scatter(point) arrow, self = vpl.annotate(point, point, np.array([0, 0, 1]), text_color="green", arrow_color="purple") assert np.all(self.color == vpl.colors.as_rgb_a("green")[0]) assert self.text == str(point) assert (self.position - point == (0, 0, 3)).all() assert np.all(arrow.color == vpl.colors.as_rgb_a("purple")[0])
def test(): import vtkplotlib as vpl # self = vpl.text3d("some text", follow_cam=True) point = np.array([1, 2, 3]) vpl.scatter(point) arrow, text = vpl.annotate(point, point, np.array([0, 0, 1])) globals().update(locals()) vpl.show()
def button_pressed_cb(self): """Plot commands can be called in callbacks. The current working figure is still self.figure and will remain so until a new figure is created explicitly. So the ``fig=self.figure`` arguments below aren't necessary but are recommended for larger, more complex scenarios. """ # Randomly place a ball. vpl.scatter(np.random.uniform(-30, 30, 3), color=np.random.rand(3), fig=self.figure) # Reposition the camera to better fit to the balls. vpl.reset_camera(self.figure) # Without this the figure will not redraw unless you click on it. self.figure.update()
def test_doc_11(self): import vtkplotlib as vpl import numpy as np # Create several balls. points = np.random.uniform(-30, 30, (30, 3)) vpl.scatter(points, color=np.random.random(points.shape)) vpl.annotate(points, "This ball is the highest", np.array([0, 0, 1]), text_color="k", arrow_color="orange") vpl.annotate(points, "This ball is the lowest", np.array([0, 0, -1]), text_color="rust", arrow_color="hunter green")
def test_qfigure2(self): fig = vpl.QtFigure2("a qt widget figure") self.assertIs(fig, vpl.gcf()) vpl.scatter(np.arange(9).reshape((3, 3)).T) vpl.quick_test_plot() fig.add_all() fig.show(block=False) fig.qapp.processEvents() for i in fig.view_buttons.buttons: i.released.emit() fig.qapp.processEvents() time.sleep(.1) fig.screenshot_button.released.emit() fig.show_plot_table_button.released.emit() fig.show()
def test(): import vtkplotlib as vpl points = np.random.uniform(-10, 10, (30, 3)) # for i in range(3): # self = vpl.cursor(np.array([5, 0, 0]) * i, radius=4) colors = vpl.colors.normalise(points) radii = np.abs(points[:, 0])**.5 vpl.scatter( points, color=colors, radius=radii, use_cursors=False, )[0] self = vpl.scatter(points, color=colors, radius=radii, use_cursors=True)[0] # self.point += np.array([10, 0, 0]) globals().update(locals()) vpl.show()
def test_read_write(path): path = Path(path) from vtkplotlib._get_vtk import vtk path.parent.mkdir(parents=True, exist_ok=True) polydata = vpl.scatter([hash(path)] * 3).polydata.vtk_polydata for i in range(2): # Do write test twice. 1 to check write and 2 to check overwriting. self = PathHandler(path, "w") with self: writer = vtk.vtkPolyDataWriter() writer.SetFileName(self.access_path) writer.SetInputData(polydata) assert writer.Write() assert os.path.exists(self.py_access_path) assert self.path.exists() with PathHandler(path, "r") as self: reader = vtk.vtkPolyDataReader() reader.SetFileName(self.access_path) reader.Update() read_polydata = reader.GetOutput() reader.CloseVTKFile() dicts = [ vpl.PolyData(pd).__getstate__() for pd in (polydata, read_polydata) ] for key in dicts[0]: # ascii read/write isn't lossless so np.allclose() is needed here assert np.all(dicts[0][key] == dicts[1][key]) \ or np.allclose(dicts[0][key], dicts[1][key]) os.remove(str(path)) return self
def test_save(): plots = vpl.scatter(np.random.uniform(-10, 10, (30, 3))) path = TEST_DIR / "name.png" if path.exists(): os.remove(str(path)) vpl.save_fig(path) assert path.exists() array = vpl.screenshot_fig(magnification=2) assert array.shape == tuple(i * 2 for i in vpl.gcf().render_size) + (3, ) 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 # .assertEqual(vpl.screenshot_fig(pixels=shape).shape, # shape[::-1] + (3,)) vpl.close() return array
def test_qfigure2(): fig = vpl.QtFigure2("a QWidget figure") fig.setWindowTitle(fig.window_name) assert fig is vpl.gcf() plot = vpl.scatter(np.arange(9).reshape((3, 3)).T)[0] vpl.quick_test_plot() fig.add_all() fig.show(block=False) fig.qapp.processEvents() for i in fig.view_buttons.buttons: i.released.emit() fig.qapp.processEvents() time.sleep(.1) if not VTKPLOTLIB_WINDOWLESS_TEST: fig.screenshot_button.released.emit() fig.show_plot_table_button.released.emit() fig.show(block=False) for plot in fig.plot_table.rows: fig.plot_table.rows[plot].text.released.emit() fig.qapp.processEvents() assert not plot.visible assert np.allclose(vpl.screenshot_fig(fig=fig), np.array(255) * fig.background_color, atol=1.) for plot in fig.plot_table.rows: fig.plot_table.rows[plot].text.released.emit() fig.qapp.processEvents() assert plot.visible fig.plot_table.close()
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()
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 # fig.add_plot(arrow) also does the same thing # Finally when your ready to view the plot call show. Like before you can # do this one of several ways # 1) fig.show() # 2) vpl.show() # equivalent to gcf().show() # 3) vpl.show(fig=fig)
def test_arrow(self): points = np.random.uniform(-10, 10, (2, 3)) vpl.scatter(points) vpl.arrow(*points, color="g") vpl.show()
def test_legend(): import vtkplotlib as vpl self = vpl.legend(None, fig=None) assert self.fig is None assert self.length == 0 vpl.gcf().add_plot(self) self.set_entry(label="Blue Square", color="blue") sphere = vpl.scatter([0, 5, 10], color="g", fig=None, label="Ball") self.set_entry(sphere, color="b") self.set_entry( sphere, "Green ball", ) rabbit = vpl.mesh_plot(vpl.data.get_rabbit_stl()) self.set_entry(rabbit, "rabbit") rabbit_wire = vpl.plot(rabbit.vectors, color=rabbit.vectors[:, :, 0], label="octopus") self.set_entry(rabbit_wire) assert self.legend.GetEntryString(self.length - 1) == "octopus" # self.set_entry(vpl.quiver(np.zeros(3), np.array([-1, 0, 1])), "right") self.set_entry(None, label="shark", icon=vpl.data.ICONS["Right"]) for size in ((.3, .4), (.3, .4, 0)): self.size = size assert np.array_equal(self.size, [.3, .4, 0]) position = np.array(1) - self.size self.position = position assert np.array_equal(self.position, position) with pytest.raises(TypeError): self.set_entry(object()) length = self.length for i in range(2): eggs = vpl.text3d("eggs", label="eggs") self.add_plots([eggs]) # Re-adding labels shouldn't cause the legend to grow assert self.length == length + 1 vpl.text("text") auto_legend = vpl.legend(position=(0, 0)) auto_legend_no_label = vpl.legend(position=(0, .7), allow_no_label=True, allow_non_polydata_plots=True, color=(.2, .3, .4, .5)) assert auto_legend_no_label.color == (.2, .3, .4) assert auto_legend_no_label.opacity == .5 self.set_entry(vpl.scatter(np.arange(12).reshape((-1, 3)), label="scatter"), label="fish", index=self.length + 3) self.add_plots(vpl.gcf().plots)
def test_zoom(): vpl.scatter(np.random.uniform(-20, 20, (30, 3)), color="r") vpl.show(block=False) balls_to_ignore = vpl.scatter(np.random.uniform(-50, 50, (30, 3))) vpl.text("This should be ignored by zoom.") vpl.zoom_to_contents(plots_to_exclude=balls_to_ignore)