0

I have a numpy array which I initialized outside the loop using np.zeros. This array is updated using some function inside a for a loop. I wish to plot the array as it changes with each iteration.

Most of the answers I have seen here are for lists and not ndarrays. I have seen the following links. Some of them I have tried to modify for my purpose but to no avail.

How to update a plot in matplotlib?

https://github.com/stsievert/python-drawnow/blob/master/drawnow/drawnow.py @Scott Sievert, I saw your code too. But unfortunately, I haven't been able to figure out how to modify it.

Real-time plotting using matplotlib and kivy in Python

Real-time plotting using matplotlib and kivy in Python

Real time trajectory plotting - Matplotlib

https://realpython.com/python-matplotlib-guide/

https://gist.github.com/vaclavcadek/66c9c61a1fac30150514a665c4bcb5dc

http://jakevdp.github.io/blog/2012/08/18/matplotlib-animation-tutorial/

Matplotlib: Animate 2D Array

So basically I want to see the value of the ndarray y in real-time. (see the code below)

I am running it as a script.@Scott Staniewicz

from numpy.random import random_sample
from numpy import arange, zeros
x = arange(0, 10)
y = zeros((10, 1))
for i in range(10):
    y[i] = sin(random_sample())
Tejas Shetty
  • 685
  • 6
  • 30
  • 1
    Do you have any samples of code that are almost working, but not quite? I've seen a couple ways to plot updating images of ndarrays, some more sophisticated, but harder to get working, so it'll be helpful to see what you're going for – Scott Staniewicz Aug 01 '19 at 07:16
  • 1
    Also it'll be helpful to know how you're trying to run this code: e.g. as a script, in an iPython terminal, in a jupyter notebook... some of these situations are a lot easier to get working than others – Scott Staniewicz Aug 01 '19 at 07:18
  • @ScottStaniewicz I have added the info in the edits – Tejas Shetty Aug 01 '19 at 07:27

2 Answers2

1

Disclaimer : I am quite sure my answer is not the most optimal, but this is what I could achieve for now.

Modifying @Scott (Scott Sievert) answer and using his drawnow Github package I have put together this answer. I didn't install drawnow Github package . Instead I just copied drawnow.py into my folder. (This is because I didn't find any way to install it via conda. I didn't wan't to use PyPi)

from numpy.random import random_sample
from numpy import arange, zeros
from math import sin
from drawnow import drawnow
from matplotlib import use
from matplotlib.pyplot import figure, axes, ion
from matplotlib import rcParams
from matplotlib.pyplot import style
from matplotlib.pyplot import cla, close
use("TkAgg")
pgf_with_rc_fonts = {"pgf.texsystem": "pdflatex"}
rcParams.update(pgf_with_rc_fonts)
style.use('seaborn-whitegrid')

max_iter = 10**(3)  # 10**(5)  # 10**(2)
y = zeros((max_iter, 1))


def draw_fig():
    # can be arbitrarily complex; just to draw a figure
    # figure() # don't call!
    scott_ax = axes()
    scott_ax.plot(x, y, '-g', label='label')
    # cla()
    # close(scott_fig)
    # show() # don't call!


scott_fig = figure()  # call here instead!
ion()
# figure()  # call here instead!
# ion()    # enable interactivity
x = arange(0, max_iter)
for i in range(max_iter):
    # cla()
    # close(scott_fig)
    y[i] = sin(random_sample())
    drawnow(draw_fig)
Tejas Shetty
  • 685
  • 6
  • 30
0

The most basic version would look like

import numpy as np
import matplotlib.pyplot as plt

x = np.arange(0, 10)
y = np.zeros((10, 1))

fig = plt.figure()
line, = plt.plot(x,y)
plt.ylim(-0.2,1.2)

for i in range(10):
    y[i] = np.sin(np.random.random_sample())
    line.set_data(x[:i+1], y[:i+1])
    plt.pause(1)

plt.show()
ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712
  • Nice, short and sweet. Only one needs to keep in mind to clear the axes (using plt.cla()), close the figure (using plt.close(fig)), if one uses it repeatedly. – Tejas Shetty Aug 03 '19 at 09:52
  • What is the effect of `plt.show()`? I run it without `plt.show()` and it seems to make no difference. – Gilfoyle Apr 19 '23 at 12:11