Wednesday, June 28, 2017

Matplotlib animation not showing

Leave a Comment

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 

2 Answers

Answers 1

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

Answers 2

It seems that the library is exposed to some changes or it is not installed properly. Simply update the library as below,

For Ubuntu/Debian OS open a terminal and type:

                    sudo apt-get install python-matplotlib 

For windows open a command line and type:

                    python -m pip install -U pip setuptools                     python -m pip install matplotlib 

See if that works.

Source: https://matplotlib.org/users/installing.html

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment