Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matplotlib animation not showing

When I try this on my computer at home, it works, but not on my computer at work. Here's the code

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


def update_line(num, gen, line):
    data = gen.vals_queue.get()
    data = np.array(data)
    line.set_data(data[..., :num])
    return line,


class Generator(multiprocessing.Process):
    def __init__(self):
        self.vals = [[], []]
        super(Generator, self).__init__()
        self.vals_queue = multiprocessing.Queue()

    def run(self):
        while True:
            self.vals[0].append(np.random.rand())
            self.vals[1].append(np.random.rand())
            self.vals_queue.put(self.vals)


if __name__ == '__main__':
    gen = Generator()
    gen.start()
    fig1 = plt.figure()
    l, = plt.plot([], [], 'r-')
    plt.xlim(0, 1)
    plt.ylim(0, 1)
    plt.xlabel('x')
    plt.title('test')
    print 11111111111111
    sys.stdout.flush()
    line_ani = animation.FuncAnimation(fig1, update_line, frames=None, fargs=(gen, l),
                                       interval=50, blit=True, repeat=False)
    print 222222222222222222222222
    sys.stdout.flush()
    plt.show()
    print 3333333333333333333333333
    sys.stdout.flush()

And the output I see is

11111111111111
222222222222222222222222
3333333333333333333333333

The application does not exit, it just hangs there, but no figure pops up. I run it from Linux terminal. My version of matplotlib is matplotlib-2.0.0-1.x86_64

Also, I've got this at work (problematic one)

CentOS Linux release 7.2.1511 (Core) 
echo $SHELL
/bin/bash
echo $BASH_VERSION
4.2.46(1)-release
Python 2.7.12
like image 952
Baron Yugovich Avatar asked Jun 19 '17 16:06

Baron Yugovich


People also ask

How do I show an animation in Matplotlib?

Animations in Matplotlib can be made by using the Animation class in two ways: By calling a function over and over: It uses a predefined function which when ran again and again creates an animation. By using fixed objects: Some animated artistic objects when combined with others yield an animation scene.

Does Jupyter notebook support animation?

The resulting animations are html video files embedded within the Jupyter notebook. The videos can be viewed in any modern web browser. Since they don't require an active Python kernal, the animations can be seen when the even when notebook is viewed as a static HTML web page.

How do I save an animation in Python?

To save an animation, we can use Animation. save() or Animation.


Video Answer


1 Answers

It is really hard to reproduce this problem, so I'll try to give some general advises and try to guess the actual root of the problem.

First of all, it is in your best interest to use virtualenvs, if you aren't using them already. You will have a requirements.txt file in your project and will freeze the requirements from your home computer (the one that works) into requirements.txt, then will create a new virtualenv on the computer at work and finally install the requirements. That way you will be sure that you have the same versions of all packages on both computers.

After that you should try and see if it works. If it doesn't please try these things and provide more details:

  1. Do you see any errors or warnings when you run it on the computer at work?
  2. Can you do very basic plots using matplotlib? Like this one:

    import matplotlib.pyplot as plt plt.plot([1, 2, 3, 4]) plt.ylabel('some numbers') plt.show()

  3. If the example from 2 doesn't work, try to replace plt.show() with plt.savefig('numbers.png') and see if the figure is successfully saved. If that's the case, then you have some problems with matplotlib's interactivity. If you can't see a file named numbers.png, then probably there is something wrong with matplotlib's installation in general, not just the animation part. Or maybe with the installation of some package matplotlib relies on, like Tkinter, for instance.

Instead of going into further hypothetical scenarios, I'll stop here and wait for more details.

p.s. Links you may find useful if there is problem with showing the generated plots/animations in a window:

How can I set the 'backend' in matplotlib in Python?

http://matplotlib.org/faq/usage_faq.html#what-is-a-backend

like image 127
giliev Avatar answered Sep 23 '22 23:09

giliev