简体   繁体   English

Matplotlib动画没有显示

[英]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. 我从Linux终端运行它。 My version of matplotlib is matplotlib-2.0.0-1.x86_64 我的matplotlib版本是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. 首先,如果您还没有使用virtualenvs ,那么使用virtualenvs符合您的最佳利益。 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. 您将在项目中有一个requirements.txt文件,并将要求从您的家用计算机(有效的)冻结到requirements.txt ,然后在工作的计算机上创建一个新的virtualenv ,最后安装要求。 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 ? 你能用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. 如果2中的示例不起作用,请尝试将plt.show()替换为plt.savefig('numbers.png')并查看图形是否已成功保存。 If that's the case, then you have some problems with matplotlib's interactivity. 如果是这种情况,那么你在matplotlib的交互性方面遇到了一些问题。 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. 如果你看不到名为numbers.png的文件,那么matplotlib的安装可能有些问题,而不仅仅是动画部分。 Or maybe with the installation of some package matplotlib relies on, like Tkinter, for instance. 或者也许安装一些matplotlib包依赖,比如Tkinter。

Instead of going into further hypothetical scenarios, I'll stop here and wait for more details. 我会停在这里等待更多细节,而不是进一步假设的情景。

ps Links you may find useful if there is problem with showing the generated plots/animations in a window: ps如果在窗口中显示生成的绘图/动画有问题,您可能会发现有用的链接:

How can I set the 'backend' in matplotlib in Python? 如何在Python中设置matplotlib中的“后端”?

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

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: 对于Ubuntu / Debian OS打开一个终端并输入:

                    sudo apt-get install python-matplotlib

For windows open a command line and type: 对于Windows打开命令行并键入:

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

See if that works. 看看是否有效。

Source: https://matplotlib.org/users/installing.html 资料来源: https//matplotlib.org/users/installing.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM