Plotting

Getting good plots to see what’s going on can be really helpful. Here’s a few tools we find invaluable for thinking about this kind of data.

Classes:

AnimAcross([ratio, sz, columns, asp])

A context-manger for making a bunch of subplots, laid out in a grid from left to right, top to bottom.

AnimGif([format, bbox_inches, duration])

A context-manger for making animated gifs from matplotlib. It is inefficient but reliable. Usage is best explained by example::.

Functions:

lutup4(A, B, C, D[, sc, normstyle])

Combine 4 real-valued images into one RGB image.

class bardensr.plotting.AnimAcross(ratio=0.8, sz=4, columns=None, asp=1.0)

A context-manger for making a bunch of subplots, laid out in a grid from left to right, top to bottom:

import matplotlib.pyplot as plt
import bardensr.plotting
with bardensr.plotting.AnimAcross(columns=3) as a:
    for i in range(10):
        ~a # invert a to indicate that we want to make a new frame
        # do some matplotlib stuf
        plt.plot([0],[i],'rx')
        plt.ylim(0,10)

Arguments:

  • ratio – controls how close subplots are together

  • sz – controls how big subplots are

  • columns – how many columns of subplots

  • asp – controls aspect ratio of subplots

NOTE. matplotlib.pyplot.colorbar doesn’t play nicely with the way these plots are laid out (it tries to create a new subplot, but doesn’t know where to put it because the subplots aren’t laid out until the end of the context manager). You can use a pattern like a.cb(plt.imshow(foo)) to put a colorbar associated with a mappable (such as is returned by imshow) next to the current subplot.

class bardensr.plotting.AnimGif(format='png', bbox_inches='tight', duration=250)

A context-manger for making animated gifs from matplotlib. It is inefficient but reliable. Usage is best explained by example:

import matplotlib.pyplot as plt
import bardensr.plotting
with bardensr.plotting.AnimGif(duration=250) as a:
    for i in range(10):
        # do some matplotlib stuf
        plt.plot([0],[i],'rx')
        plt.ylim(0,10)
        a() # call a to indicate that we're doine with this frame
with open('foo.gif','wb') as f:
    f.write(a.gif) # write gif to file
~a # return IPython.display.Image object that can be viewed in notebook

Arguments:

  • format – what kind of images to save matplotlib figures as (they’ll all get converted to gif eventually tho)

  • bbox_inches – what bbox_inches to use when calling matplotlib.pyplot.save

  • duration – how long each frame of the gif be, in milliseconds

bardensr.plotting.lutup4(A, B, C, D, sc=0.5, normstyle='none')

Combine 4 real-valued images into one RGB image.

Input:

  • A (M0 x M1 x M2 x … M(n-1))

  • B (M0 x M1 x M2 x … M(n-1))

  • C (M0 x M1 x M2 x … M(n-1))

  • D (M0 x M1 x M2 x … M(n-1))

  • sc, a scalar that helps normalize the images

  • normstyle, indicating how the normalization should be done

Output is an array with dimensions

(M0 x M1 x M2 x … M(n-1) x 3)

and dtype uint8, representing an image. In this new image,

  • signal from “A” is blueish (contributing 1 unit of red, 2 units of green, and 4 units of blue)

  • signal from “B” is greenish (contributing one unit of red, four units of green, and two units of blue)

  • signal from “C” is yellowish (contributing three units of red, three units of green, and one unit of blue)

  • signal from “D” is reddish (contributing four units of red, two units of green, and one unit of blue)

There are different kinds of normalization that can be applied to the images before they are combined into an RGB image:

  • “each” – A,B,C,D are each normalized so that their minimum is 0 and their maximum is sc

  • “all” – A,B,C,D is normalized together so that the minimum over all of them is zero and the maximum over all of them is sc

  • “none” – no normalization

After normalization, each real-valued image contributes its portion to the red, green, and blue channels (as described above), the result is clipped to lie between 0 and 1, then multiplied by 255, and finally cast to uint32.