Python Tutorial: Advanced plotting

preview_player
Показать описание

----

Efficiently and comprehensively visualizing your data is key to successful image analysis.

Consider this loaf of bread - it’s a 3D volume that looks absolutely delicious. But what's inside the loaf? Is it bananas? Blueberries? Walnuts? This single 2D image cannot answer the question.
Instead, you would have to slice it up and look at those pieces individually. The concept for 3D images is exactly the same: to explore your multidimensional array you must visualize many simpler slices of it.

It's inefficient to plot each of these slices one by one. A better way is to display many at once. To do this, we'll leverage PyPlot's subplots() function, which creates a grid of axis objects based on the number of rows and columns you specify.

When you call pyplot dot subplots(), it returns a figure object and an array of axis handles. In this case, the axes array has a shape of one by three.

To draw images on each subplot, we will call the imshow() function directly from the axis object, rather than calling it through PyPlot. Here, we're plotting the first slice of vol with a gray colormap.

After the first slice is drawn, repeat the process for the other slices.

Finally, we can clean up and render the figure. Just like before, we want to turn off the ticks and labels by calling the axis() method. However, since we will do the same thing to each subplot, a for loop is more efficient than writing down each command. If you had many images to draw, you could insert the drawing step into a for loop as well.

Finally, we render the image using pyplot dot show().

And we are greeted with a nice set of images that move sequentially through the chest volume.

There are actually multiple ways you can slice a 3D volume into 2D images.

The simplest way is to choose a frame along the first dimension and plot the second and third against each other.

If you instead selected a slice along the row dimension, you would get a second perspective. In this case, we are plotting head-to-toe versus left-to-right.

Finally, you could plot the first and second axes against each other, yielding a third view.

When looking at human anatomy, these different views have special names: the axial, coronal, and sagittal planes. Knowing in which plane a dataset is stacked can help you navigate more efficiently.

Many datasets do not have equal sampling rates across all dimensions. In these cases, you will want to stretch the pixels along one side to account for the differences. The amount of stretching needed is determined by the aspect ratio.

Here we've decided to plot a slice with data from the first and second dimensions. To determine the aspect ratio, we first get the sampling rates along each dimension from the metadata dictionary.

Then, we divide the sampling rate of the first dimension by the sampling rate of the second.

When we call imshow(), we pass this ratio to the aspect argument.

This results in a properly proportioned image. Failing to adjust the aspect would have resulted in a distorted image.

Now let's practice using these techniques to plot 3D images.
Рекомендации по теме