0

I am trying to draw an online figure via matplotlib in jupyter notebook. The following code works fine when is called in terminal.

import matplotlib.pyplot as plt
import numpy as np

plt.ion()
fig, ax = plt.subplots()
x, y = [],[]
sc = ax.scatter(x,y)
y_max = .1
x_max = .1
plt.xlim(0,x_max)
plt.ylim(0,y_max)

plt.draw()
for i in range(1000):
    x.append(i*0.001)
    y.append(i*i*0.001)
    if y[-1]  > y_max:
        plt.ylim(0,y_max)
        y_max = y[-1]
    if x[-1]  > x_max:
        plt.xlim(0,x_max)
        x_max = x[-1]

    sc.set_offsets(np.c_[x,y])
    fig.canvas.draw_idle()
    plt.pause(0.1)
    plt.waitforbuttonpress()

Although, when I run this code by adding %matplotlib inline, it does not show me the progress and I just see the final figure.

Also, I read the other posts, e.g.: Inline animations in Jupyter and Animation in iPython notebook

which both use matplotlib.animation.FuncAnimation. This solution needs the data given, before starting the drawing; however, I need a solution for the case that data is generated inside a loop.

I appreciate any help or comment.

Afshin Oroojlooy
  • 1,158
  • 3
  • 17
  • 37
  • You didn't read the answers thoroughly. Not all options use `FuncAnimation`. E.g. you could simply use the `%matplotlib notebook` backend. Or the `display` approach. Besides, there is nothing wrong with `FuncAnimation` in case you calculate the values in a loop (Except that the loop will be replaced by the animating function). – ImportanceOfBeingErnest Mar 29 '19 at 20:36

0 Answers0