Skip to article frontmatterSkip to article content

You can change a visualization’s appearance by accessing and modifying its transfer function.

import example_utils
from vapor import session, renderer, dataset, camera, transferfunction
from vapor.utils import histogram

ses = session.Session()
data = example_utils.OpenExampleDataset(ses)

ren = data.NewRenderer(renderer.VolumeRenderer)
ses.GetCamera().LookAt((32, 120, 120), (32, 32, 32))
ses.Show()
Vapor 3.10.0
Python 3.9.19 (/opt/anaconda3/envs/vapor)
OpenGL 4.1 Metal - 89.3
UNSUPPORTED (log once): POSSIBLE ISSUE: unit 3 GLD_TEXTURE_INDEX_3D is unloadable and bound to sampler type (Float) - using zero texture because texture unloadable
<PIL.Image.Image image mode=RGB size=600x480>

Changing Opacities

Vapor’s transferFunctionWidget allows you to adjust the opacity points of a renderer as you would within Vapor’s GUI.

from vapor.utils import histogram
histogram.transferFunctionWidget(ses, ren)
Loading...

If you do not have access to the widget, we provide static options for changing the opacities as well. We created a volume rendering however it is fully opaque. We can use a transfer function to adjust the visible portions. Before we adjust the opacity map of the TF, we get a histogram to help us determine what we want to hide.

histogram.ShowMatPlotLibHistogram(ses, ren)
<Figure size 640x480 with 1 Axes>

Usually we want to hide the most common value so below we construct an opacity map that accomplishes this.

# List of x,y pairs where x is the data value and y is the opacity for that data value
opacities = [(-0.3, 1), (-0.1, 0), (0.1, 0), (0.3, 1)]

We can get the matplotlib histogram plot and add our opacity map to it to compare.

plt = histogram.GetMatPlotLibHistogram(ses, ren)
plt.plot(*zip(*opacities))
plt.show()
<Figure size 640x480 with 1 Axes>

Now we apply the map to the transfer function

# Renderers can have multiple transfer functions.
# GetPrimaryTransferFunction returns the one that is usually the most useful.
# You can use `tf.GetTransferFunction(var_name)` to get other transfer functions.
tf = ren.GetPrimaryTransferFunction()
tf.SetOpacityControlPoints(opacities)
ses.Show()
<PIL.Image.Image image mode=RGB size=600x480>

You can adjust the colormap in a similar fashion. Use help(tf) for more information. Vapor includes a list of built-in colormaps and these can be applied with tf.LoadBuiltinColormap(name)

Builtin Colormaps

tf.LoadBuiltinColormap("Sequential/BlackBodyExtended")
ses.Show()
<PIL.Image.Image image mode=RGB size=600x480>

List of All Builtin Colormaps


ses.DeleteRenderer(ren)
ren = data.NewRenderer(renderer.TwoDDataRenderer)
tf = ren.GetPrimaryTransferFunction()

for cmap in transferfunction.TransferFunction.ListBuiltinColormaps():
    tf.LoadBuiltinColormap(cmap)

    print(cmap)
    tf.ShowMatPlotLibColorbar()
Highlighting/topo
<Figure size 900x150 with 2 Axes>
Highlighting/oxy
<Figure size 900x150 with 2 Axes>
Diverging/tarn
<Figure size 900x150 with 2 Axes>
Diverging/curl
<Figure size 900x150 with 2 Axes>
Diverging/CoolWarmSmooth
<Figure size 900x150 with 2 Axes>
Diverging/PurpleWhiteOrange
<Figure size 900x150 with 2 Axes>
Diverging/diff
<Figure size 900x150 with 2 Axes>
Diverging/GreenWhiteRed
<Figure size 900x150 with 2 Axes>
Diverging/CoolWarmBent
<Figure size 900x150 with 2 Axes>
Diverging/BlueWhiteGold
<Figure size 900x150 with 2 Axes>
Diverging/CoolWarm
<Figure size 900x150 with 2 Axes>
Diverging/GreenWhitePurple
<Figure size 900x150 with 2 Axes>
Diverging/balance
<Figure size 900x150 with 2 Axes>
Diverging/delta
<Figure size 900x150 with 2 Axes>
Sequential/BlackBody
<Figure size 900x150 with 2 Axes>
Sequential/thermal
<Figure size 900x150 with 2 Axes>
Sequential/tempo
<Figure size 900x150 with 2 Axes>
Sequential/speed
<Figure size 900x150 with 2 Axes>
Sequential/Kindlmann
<Figure size 900x150 with 2 Axes>
Sequential/KindlmannExtended
<Figure size 900x150 with 2 Axes>
Sequential/algae
<Figure size 900x150 with 2 Axes>
Sequential/deep
<Figure size 900x150 with 2 Axes>
Sequential/haline
<Figure size 900x150 with 2 Axes>
Sequential/turbid
<Figure size 900x150 with 2 Axes>
Sequential/rain
<Figure size 900x150 with 2 Axes>
Sequential/Rainbow
<Figure size 900x150 with 2 Axes>
Sequential/phase
<Figure size 900x150 with 2 Axes>
Sequential/BlackBodyExtended
<Figure size 900x150 with 2 Axes>
Sequential/dense
<Figure size 900x150 with 2 Axes>
Sequential/amp
<Figure size 900x150 with 2 Axes>
Sequential/ice
<Figure size 900x150 with 2 Axes>
Sequential/matter
<Figure size 900x150 with 2 Axes>
Sequential/BlackWhite
<Figure size 900x150 with 2 Axes>
Sequential/solar
<Figure size 900x150 with 2 Axes>