Matplotlib animation not showing

2024/11/15 0:33:39

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
Answer

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

https://en.xdnf.cn/q/72475.html

Related Q&A

Extracting Fields Names of an HTML form - Python

Assume that there is a link "http://www.someHTMLPageWithTwoForms.com" which is basically a HTML page having two forms (say Form 1 and Form 2). I have a code like this ...import httplib2 from …

Best way to combine a permutation of conditional statements

So, I have a series of actions to perform, based on 4 conditional variables - lets say x,y,z & t. Each of these variables have a possible True or False value. So, that is a total of 16 possible per…

Fast way to get N Min or Max elements from a list in Python

I currently have a long list which is being sorted using a lambda function f. I then choose a random element from the first five elements. Something like:f = lambda x: some_function_of(x, local_variabl…

Continue if else in inline for Python

I have not been able to find the trick to do a continue/pass on an if in a for, any ideas?. Please dont provide explicit loops as solutions, it should be everything in a one liner.I tested the code wi…

HTTPError: HTTP Error 403: Forbidden on Google Colab

I am trying to download MNIST data in PyTorch using the following code:train_loader = torch.utils.data.DataLoader(datasets.MNIST(data,train=True,download=True,transform=transforms.Compose([transforms.T…

Pandas partial melt or group melt

I have a DataFrame like this>>> df = pd.DataFrame([[1,1,2,3,4,5,6],[2,7,8,9,10,11,12]], columns=[id, ax,ay,az,bx,by,bz]) >>> dfid ax ay az bx by bz 0 1 1 2 3 4 5 6…

How do I detect when my window is minimized with wxPython?

I am writing a small wxPython utility.I would like to use some event to detect when a user minimizes the application/window.I have looked around but did not find an event like wx.EVT_MINIMIZE that I co…

Pandas: How to select a column in rolling window

I have a dataframe (with columns a, b, c) on which I am doing a rolling-window.I want to be able to filter the rolling window using one of the columns (say a) in the apply function like belowdf.rolling…

What is the fastest way in Cython to create a new array from an existing array and a variable

Suppose I have an arrayfrom array import array myarr = array(l, [1, 2, 3])and a variable: myvar = 4 what is the fastest way to create a new array:newarray = array(l, [1, 2, 3, 4])You can assume all ele…

Subclassing and built-in methods in Python

For convenience, I wanted to subclass socket to create an ICMP socket:class ICMPSocket(socket.socket):def __init__(self):socket.socket.__init__(self, socket.AF_INET,socket.SOCK_RAW,socket.getprotobynam…