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="http://i.stack.imgur.com/dfbzx.png" rel="nofollow"><img src="http://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="http://i.stack.imgur.com/iaw2u.png" rel="nofollow"><img src="http://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>

<h2>The fix</h2>

<p>Replace the line</p>

<pre><code>ax.plot(pos[:, 0], pos[:, 1], pos[:, 2])
</code></pre>

<p>with</p>

<pre><code>ax.plot(list(pos[:, 0]), list(pos[:, 1]), list(pos[:, 2]))
</code></pre>

<p>and it will work as expected for global <code>pos</code>.</p>

<h2>Explanation</h2>

<p>The problem is that <code>ax.plot(xlist, ylist, zlist)</code> doesn't immediately plot the data. It merely stores references to <code>xlist</code>, <code>ylist</code>, and <code>zlist</code> and uses the data to construct the plot when <code>plt.show()</code> is called. Next, <code>pos[:, 0]</code>, <code>pos[:, 1]</code>, and <code>pos[:, 2]</code> do not return the corresponding columns by value. Instead they return some proxy objects that reference the original matrix.</p>

<p>As a result, actual plotting is performed using the data that ends up in the matrix after the last iteration and all the plots coincide. By wrapping each of <code>pos[:, 0]</code>, <code>pos[:, 1]</code>, and <code>pos[:, 2]</code> in a <code>list()</code> we force the column data to be copied, decoupling the plots from each other.</p>

python matplotlib