def test_opacity_transfer_functions(): n = 256 mapping = pyvista.opacity_transfer_function('linear', n) assert len(mapping) == n mapping = pyvista.opacity_transfer_function('sigmoid_10', n) assert len(mapping) == n with pytest.raises(KeyError): mapping = pyvista.opacity_transfer_function('foo', n) with pytest.raises(RuntimeError): mapping = pyvista.opacity_transfer_function(np.linspace(0, 1, 2*n), n) foo = np.linspace(0, n, n) mapping = pyvista.opacity_transfer_function(foo, n) assert np.allclose(foo, mapping) foo = [0,0.2,0.9,0.2,0.1] mapping = pyvista.opacity_transfer_function(foo, n, interpolate=False) assert len(mapping) == n foo = [3, 5, 6, 10] mapping = pyvista.opacity_transfer_function(foo, n) assert len(mapping) == n
############################################################################### # It's also possible to use your own transfer function that will be linearly # mapped to the scalar array plotted. For example, we can create an opacity # mapping as: opacity = [0, 0.2, 0.9, 0.6, 0.3] ############################################################################### # When given a minimized opacity mapping like that above, PyVista interpolates # it across a range of how many colors are shown when mapping the scalars. # If ``scipy`` is available, then a quadratic interpolation is used - # otherwise, a simple linear interpolation is used. # Curious what that opacity transfer function looks like? You can fetch it: # Have PyVista interpolate the transfer function tf = pv.opacity_transfer_function(opacity, 256).astype(float) / 255. import matplotlib.pyplot as plt plt.plot(tf) plt.title('My Interpolated Opacity Transfer Function') plt.ylabel('Opacity') plt.xlabel('Index along scalar mapping') plt.show() ############################################################################### # That opacity mapping will have an opacity of 0.0 at the minimum scalar range, # a value or 0.9 at the middle of the scalar range, and a value of 0.3 at the # maximum of the scalar range: mesh.plot(opacity=opacity)
grid = pv.StructuredGrid(x, y, z) print(grid.points.shape) # let's define array d which will contain count values # for each point of the grid. Now array d is filled with random # integer values d = np.random.randint(low=1, high=100, size=grid.points.shape) # or you can fill the scalar values with zeros # d = np.zeros_like(grid.points) grid.plot(show_edges=True, scalars=d[:, 1], stitle='Counts (CPM)') # next example: opacity transfer function (opacity of each cell depends # on the scalar value). Example from pyvista project page: # https://docs.pyvista.org/examples/02-plot/opacity.html opacity = [0, 0.2, 0.9, 0.6, 0.3] # Have PyVista interpolate the transfer function tf = pv.opacity_transfer_function(opacity, 100).astype(float) / 100. plt.plot(tf) plt.title('My Interpolated Opacity Transfer Function') plt.ylabel('Opacity') plt.xlabel('Index along scalar mapping') plt.show() grid.plot(show_edges=True, scalars=d[:, 1], opacity=opacity, stitle='Counts (CPM) with Opacity')