Matplotlib动画在PyCharm中不起作用 [英] Matplotlib animations do not work in PyCharm

查看:101
本文介绍了Matplotlib动画在PyCharm中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现了各种使用matplotlib制作动画的短文件。通常,从命令行运行它们时效果很好,但是在PyCharm中,我只能得到静止帧。

I've found various short files that produce animations using matplotlib. In general, they work fine when run from the command line, but in PyCharm I only get a still frame.

我问的是与Matplotlib在IDE(PyCharm)中使用时不会更新图。那里贴了一个答案,这似乎适用于原始提问者。当我从命令行运行该代码时,它可以正常工作。在PyCharm中,它会暂停很长一段时间(大概正在运行动画),然后显示一个静止帧(看起来像动画的开始或结束)。

I'm asking the same question as Matplotlib does not update plot when used in an IDE (PyCharm). There's an answer posted there, which seems to work for the original asker. When I run that code from the command line, it works fine. From PyCharm, it pauses for a long time (presumably running the animation) and then shows a still frame (that looks like either the beginning or end of the animation).

我正在Mac(OS 10.11.6)上通过PyCharm 2017.3.2(专业版)运行Python 3.6.2(Anaconda)。我在PyCharm中创建了一个Python项目,将该代码粘贴到.py文件中,安装了适当的库(matplotlib 2.0.2,numpy 1.13.1),然后运行了该程序。

I'm running Python 3.6.2 (Anaconda) through PyCharm 2017.3.2 (Professional) on a Mac (OS 10.11.6). I created a Python project in PyCharm, pasted that code into a .py file, installed the appropriate libraries (matplotlib 2.0.2, numpy 1.13.1), and ran the program.

我与命令行所做的唯一区别是python --version提供了以下功能:

The only difference I can see between this and what I did on the command line is that python --version there gives:

Python 3.6.0 :: Anaconda custom (x86_64)

还有什么问题吗?

推荐答案

根据答案并票证,您可以禁用在工具窗口(文件->设置->工具-> Python Scientific),我将给出此解决方案的示例。

According to this answer and this ticket, you can disable Show plots in tool window(File->Settings->Tools->Python Scientific) in Pycharm, and I will give an example for this solution.

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation

fig, ax = plt.subplots()
xdata, ydata = [], []
ln, = plt.plot([], [], 'ro')

def init():
    ax.set_xlim(0, 2*np.pi)
    ax.set_ylim(-1, 1)
    return ln,

def update(frame):
    xdata.append(frame)
    ydata.append(np.sin(frame))
    ln.set_data(xdata, ydata)
    return ln,

ani = FuncAnimation(fig, update, frames=np.linspace(0, 2*np.pi, 128),
                    init_func=init, blit=True)
plt.show()

这篇关于Matplotlib动画在PyCharm中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆