matplotlib: render into buffer / access pixel data

Sure, just use fig.canvas.tostring_rgb() to dump the rgb buffer to a string.

Similarly, there’s fig.canvas.tostring_argb() if you need the alpha channel, as well.

If you want to dump the buffer to a file, there’s fig.canvas.print_rgb and fig.canvas.print_rgba (or equivalently, print_raw, which is rgba).

You’ll need to draw the figure before dumping the buffer with tostring*. (i.e. do fig.canvas.draw() before calling fig.canvas.tostring_rgb())

Just for fun, here’s a rather silly example:

import matplotlib.pyplot as plt
import numpy as np

def main():
    t = np.linspace(0, 4*np.pi, 1000)
    fig1, ax = plt.subplots()
    ax.plot(t, np.cos(t))
    ax.plot(t, np.sin(t))

    inception(inception(fig1))
    plt.show()

def fig2rgb_array(fig):
    fig.canvas.draw()
    buf = fig.canvas.tostring_rgb()
    ncols, nrows = fig.canvas.get_width_height()
    return np.fromstring(buf, dtype=np.uint8).reshape(nrows, ncols, 3)

def inception(fig):
    newfig, ax = plt.subplots()
    ax.imshow(fig2rgb_array(fig))
    return newfig

main()

enter image description here
enter image description here
enter image description here

Leave a Comment