11

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.

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).

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.

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)

What else could be the problem?

Peter Drake
  • 443
  • 4
  • 17
  • Since the code from the linked answer runs fine for everyone, you need to think for yourself what could be different in your case - or if you have no clue, make your case as reproducible for others as possible, clearly stating how you run it, what versions you are using, posting a screenshot that allows to judge on other possible problems. – ImportanceOfBeingErnest Jan 09 '18 at 21:58
  • I've edited the question above. I'm not sure what it would be useful to take a screenshot of, especially as this is a question about animation. – Peter Drake Jan 09 '18 at 22:16
  • If the python version is different, are you sure to have the same matplotlib version installed for both python versions? Which matplotlib backend are you using in both cases? When you "ran the program", what did you do to make it run? Is there the same kind of window popping up that you would see when running a script, i.e. the same icon and title? – ImportanceOfBeingErnest Jan 09 '18 at 22:21
  • [This is how it would look for me.](https://i.stack.imgur.com/I2EMu.gif) You may try to use the same backend, namely `import matplotlib; matplotlib.use("TkAgg")` (make sure those are the first lines of the script). – ImportanceOfBeingErnest Jan 09 '18 at 22:35
  • Short story: that did the trick! Longer story: it took some work to get the version numbers to match. I essentially had to uninstall and reinstall Python because I had several different versions lying around (2 and 3, Anaconda and not). I made a fresh PyCharm project and got the same behavior. Fortunately, you found a solution while I was working on that. Thanks! I'll go post this detail (with credit to you) as an additional answer to the other question. – Peter Drake Jan 09 '18 at 23:04
  • You can post an answer to your own question. – Mr. T Jan 10 '18 at 00:39
  • So should I post @ImportanceOfBeingErnest's solution as an answer here? Alternately, should this question be deleted if the information is in the answers to the other question? I don't know Stack Overflow conventions well enough yet. – Peter Drake Jan 10 '18 at 03:39
  • Alternatives are: (a) Provide your own answer to your question (b) mark this question as duplicate of a question that contains the answer (c) delete the question (in case there is a chance that other people might have a similar problem, deletion is probably the least best choice). – ImportanceOfBeingErnest Jan 10 '18 at 08:50
  • It sounds like (b) is the right choice here, but according to [this post](https://meta.stackexchange.com/questions/118124/where-on-earth-is-the-mark-duplicate-ui) I don't yet have the ability to do that. – Peter Drake Jan 10 '18 at 17:39

2 Answers2

8

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()

image

douyu
  • 2,377
  • 2
  • 14
  • 27
2

The above (or in the link attached) did not work for me, however, i found this works well (running 2.7 with anaconda and ipython console)-

Instead of executing the script normally (using run / Shift+f10), i would first set:

%matplotlib qt5

then execute the script from pycharm console, using

runfile('/path/to/script.py')

resulting in a similar result as if i would do the same from the stand alone ipython console: hover graph

(Note - the figure is animated)

Dinari
  • 2,487
  • 13
  • 28