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 multiprocessingdef 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 11111111111111sys.stdout.flush()line_ani = animation.FuncAnimation(fig1, update_line, frames=None, fargs=(gen, l),interval=50, blit=True, repeat=False)print 222222222222222222222222sys.stdout.flush()plt.show()print 3333333333333333333333333sys.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
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:
- Do you see any errors or warnings when you run it on the computer at work?
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()
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