Advanced
Tutorial
Matplotlib
Visualization
Example 4: 3D Plotting
Introduction
In this tutorial, we will equip the macro module we created in the Example 1: Module Setup and later on adapted by enabling it to plot grayscale distributions of single slices and sequences in 2D in Example 2: 2D Plotting with a three dimensional plotting functionality.
Steps to do
The fields and commands needed have already been prepared in the second tutorial. We will just have to modify our .py file a little to make them usable. Integrate the following code into your .py file and import numpy.
BaseNetwork.py
def click3D():
clearFigure()
figure = ctx.control("canvas").object().figure()
values = [i for i in range(startSlice, endSlice + 1)]
if startSlice == endSlice:
subplot = figure.add_subplot(111, projection='3d')
subplot.bar3d(x=getX(), y=startSlice, z=0, dx=1, dy=1, dz=getY())
subplot.set_yticks(np.arange(startSlice, endSlice))
subplot.set_title(f'Slice {startSlice}')
figure.canvas.draw()
else:
clearFigure()
figure = ctx.control("canvas").object().figure()
subplot = figure.add_subplot(111, projection='3d')
for i in values:
ctx.field("SubImage.z").value = i
ctx.field("SubImage.sz").value = i
subplot.bar3d(x=getX(), y=i, z=0, dx=1, dy=1, dz=getY())
subplot.set_yticks(values)
subplot.set_title(f'Sequence from {values[0]} to {endSlice}')
ctx.field("SubImage.z").value = values[0]
figure.canvas.draw()
After saving, you should be able to reproduce results like these:
Warning:
You cannot zoom into 3D plots on a Matplotlib canvas. Try changing the viewing angle instead.
You can download the .py file below if you want.
Download Python file here.