0

I'm programming a random walk simulator in a JupyterLab Notebook. With a for loop, I want the simulator to generate multiple datasets that are then plotted on a single figure. I managed to do this. Here's my code (including the code for the random walk simulator):

#RANDOM WALK SIMULATOR

import matplotlib.pyplot as plt
import numpy as np
def random_walk(random_state_index, initial_position=(0, 0), steps=1000):
    np.random.RandomState(random_state_index)
    X, Y = [initial_position[0]], [initial_position[0]]
    for i in range(steps):
        plt.pause(0.00001)
        # Random step choice
        ways = ["up", "down", "left", "right"]
        direction = np.random.choice(ways, p = [0.3,0.2,0.25,0.25])
        if direction == "up":
            X.append(X[-1])
            Y.append(Y[-1] + 0.1)
        if direction == "down":
            X.append(X[-1])
            Y.append(Y[-1] - 0.1)
        if direction == "left":
            X.append(X[-1] - 0.1)
            Y.append(Y[-1])
        if direction == "right":
            X.append(X[-1] + 0.1)
            Y.append(Y[-1])
    return X,Y

#PLOT
fig, ax = plt.subplots()
for i in range(10):
    X,Y = random_walk(random_state_index = i)
    ax.plot(X,Y)
fig

Ahead is a link that shows what my output looks like. As you can see, I get an extra, empty plot. I'd like to get rid of it, but I'm having difficulty finding out how: OUTPUT

Thank you!

SOLUTION: I found a solution without resorting to interactive mode. Here's the solution:

%matplotlib inline
plt.show(block = True)
fig, ax = plt.subplots()
for i in range(10):
    X,Y = random_walk(random_state_index = i)
    ax.plot(X,Y)
Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
  • 1
    You need to plot in interactive mode, and remove the last `fig` in the cell. See [code and plot](https://i.stack.imgur.com/xcY4e.png). Otherwise you can use interactive mode and leave `fig`, in which case the interactive plot will show the drawing as it happens and the finished plot will show up in the notebook. To return to inline plots run `%matplotlib inline` in a cell. – Trenton McKinney Jun 21 '22 at 15:57
  • Your code works when I run Jupyter Notebook from my desktop. However, I'm using a Jupyter Notebook server that runs from the node of a computer cluster. Perhaps because of that, I'm unable to access interactive mode, unfortunately. I've emailed my IT department about this, but I'd be incredibly appreciative if anyone had a solution that didn't require access to interactive mode. – Captain Ahab Jun 21 '22 at 16:20
  • See the new duplicates for interactive mode in colab. You can't watch the plot drawn in inline mode. – Trenton McKinney Jun 21 '22 at 16:23
  • 1
    I found a solution! It was ridiculously simple. I just removed ```fig``` at the bottom of my code and every subplot appeared in the same figure. – Captain Ahab Jun 21 '22 at 17:12
  • @TrentonMcKinney I'll take a look at google colab. I'm not too familiar with it – Captain Ahab Jun 22 '22 at 06:01
  • It was my mistake, I thought you meant Google colab when you mentioned working on a computer cluster – Trenton McKinney Jun 22 '22 at 06:25
  • 1
    @TrentonMcKinney No worries. But, I appreciate the help! It's my university's computer cluster – Captain Ahab Jun 22 '22 at 16:57

1 Answers1

0

The reason you're seeing the above plot is because it is returned inline by the IPython Notebook. To prevent that from happening, use the specific draw call (which doesn't return anything) plt.show().

In other words, you're asking for the figure twice. Once at the bottom of the cell (when you write fig) and then again, by default, with the inline matplotlib drawing.

dsillman2000
  • 976
  • 1
  • 8
  • 20
  • So, would I set the argument for plt.show to True and include that before where I define the variables for my subplot, i.e. ``` plt.show(block = True); fig, ax = plt.subplots() ... ``` I did this and I get the same output as before. – Captain Ahab Jun 21 '22 at 15:56