Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Plot to image object

I can save a single plot with imshow(), since it returns an image object, like this:

image = plt.imshow(list, interpolation=None)

Later I want to create an animation of many of those images, that I save in a list and render it as a video. (If this approach is stupid, please let me know, I fear I am doing something wrong.)

anim = animation.ArtistAnimation(fig, images, interval=15, blit=True)

But I have no idea how I can do the same with a figure of two subplots. Shouldn't there also be a function that returns me an image object?

list = [[1,2,3,4,5,6,7,8],
        [1,2,3,4,5,6,7,8]]
list2 = [[1,2,3,4,5,6,7,8],
        [1,2,3,4,5,6,7,8]]

fig = plt.figure()

ax = fig.add_subplot(2, 1, 1)
ax.imshow(list, interpolation=None)

ax2 = fig.add_subplot(2, 1, 2)
ax2.imshow(list, interpolation=None)

plt.show()

Instead of showing the plot, I want to save it as an image object. My goal is to create an animation (of many plots) and render it as a video. Maybe my approach is wrong. Is there a better solution?

like image 757
Dusty Avatar asked Nov 01 '25 14:11

Dusty


2 Answers

You can save it to an "in-memory" buffer like this:

# Save PNG to memory buffer as BytesIO
from io import BytesIO
buffer = BytesIO()

plt.savefig(buffer,format='png')
PNG = buffer.getvalue()

If you mean you want a PIL/Pillow Image from that:

reloadedPILImage = Image.open(buffer)
like image 194
Mark Setchell Avatar answered Nov 04 '25 07:11

Mark Setchell


Here is an adaption of this example to work with two lists of data. Note that when the images don't all have the same range of data, the range will be taken from the first element drawn. A workaround is to explicitly set vmin and vmax to the global minimum and maximum.

from matplotlib import pyplot as plt
from matplotlib import animation
import numpy as np

x, y = np.meshgrid(np.linspace(0, 5, 30), np.linspace(0, 5, 10))
list1 = [np.sin(x + i * 0.10) * np.cos(y + i * 0.01) for i in range(50)]
list2 = [np.cos(x + i * 0.01) * np.sin(y + i * 0.10) for i in range(50)]
list1[0] = np.zeros_like(list1[0])  # test what happens when the first element has only one color

fig = plt.figure()

ax1 = fig.add_subplot(2, 1, 1)

vmin1 = min([l.min() for l in list1 ])
vmin2 = min([l.min() for l in list2 ])
vmax1 = max([l.max() for l in list1 ])
vmax2 = max([l.max() for l in list2 ])
ax2 = fig.add_subplot(2, 1, 2)
im1 = ax1.imshow(list1[0], interpolation=None, animated=True, vmin=vmin1, vmax=vmax1)
im2 = ax2.imshow(list2[0], interpolation=None, animated=True, cmap='inferno', vmin=vmin2, vmax=vmax2)

def updatefig(i):
    im1.set_array(list1[i])
    im2.set_array(list2[i])
    return im1, im2,

ani = animation.FuncAnimation(fig, updatefig, frames=len(list1), interval=20, blit=True)
plt.show()
like image 39
JohanC Avatar answered Nov 04 '25 05:11

JohanC



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!