Plot is behaving weird
<p>I am trying to plot some trajectories in 3D. I noticed that the plot function is behaving weird. </p>

<p>I defined a variable named <code>pos</code>, which is a 2 dimensional matrix. It has 3 columns, where each column represents a coordinate axis. Please see the complete code below-</p>

<pre><code>import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

max = 1.0
min = -1.0
cols = 3
goals = 4
timesteps = 20

#pos = np.zeros((timesteps, cols)) # this doesn't works hence commented

fig = plt.figure()
ax = fig.gca(projection='3d')
for i in range(goals):
    pos = np.zeros((timesteps, cols)) # this works as expected
    for t in range(timesteps):
        pos[t] = np.random.uniform(low=min, high=max, size=cols)
    ax.plot(pos[:, 0], pos[:, 1], pos[:, 2])

ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_zlabel('z')
plt.show()
</code></pre>

<p>The plot doesn't draw everthing, when <code>pos</code> is defined globally. I noticed that defining <code>pos</code> inside the <code>for</code> loop, solve the problem. It looks weird to me.</p>

<p>Below is the plot, generated from global <code>pos</code> variable (after commenting the <code>pos</code> defined inside <code>for</code> loop and keeping the global <code>pos</code> variable enabled)-
<a href="https://i.stack.imgur.com/dfbzx.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/dfbzx.png" alt="enter image description here"></a></p>

<p>Below is the plot, generated from inner <code>pos</code> variable (after commenting the global <code>pos</code> variable and keeping the <code>pos</code> defined inside <code>for</code> loop enabled)-
<a href="https://i.stack.imgur.com/iaw2u.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/iaw2u.png" alt="enter image description here"></a></p>

<p>What is the reason of for this kind of behavior?</p>