Matplotlib - 2 figures in the subtitle - 1 - animation

I have two numbers that I would like to build in the subtitle:

fig = plt.figure()
ax1 = fig.add_subplot(1,2,1)
ax2 = fig.add_subplot(1,2,2)

Suppose ax1 is filled with an animation that adds points (scatter plot). Ax2 then translates these points into a meshgrid and displays the density.

Is it possible to display the animation in subtitle 1, and upon completion add a density image to subplot2?

+3
source share
1 answer

It should be possible. Please take a look at example . You can also check the previous question:

Simple animation of 2D coordinates using matplotlib and pyplot

. :

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

def update_line(num, data, line, img):
    line.set_data(data[...,:num])
    if num == 24:
        img.set_visible(True)
    return line, img

fig1 = plt.figure()

data = np.random.rand(2, 25)
ax1=plt.subplot(211)
l, = plt.plot([], [], 'rx')
plt.xlim(0, 1)
plt.ylim(0, 1)
plt.xlabel('x')
plt.title('test')
ax2=plt.subplot(212)
nhist, xedges, yedges = np.histogram2d(data[0,:], data[1,:])
img = plt.imshow(nhist, aspect='auto', origin='lower')
img.set_visible(False)
line_ani = animation.FuncAnimation(fig1, update_line, 25, 
                                   fargs=(data, l, img),
                                   interval=50, blit=True)
line_ani.repeat = False
plt.show()
+2

Source: https://habr.com/ru/post/1614007/


All Articles