Graphic update of a 3d python graph in a loop, grid line intersection points

I update the 3D scatter plot with each iteration of the loop. When the graph is redrawn, the grid lines β€œpass” or β€œcover” the points, which makes it difficult to visualize my data. If I build one 3D plot (without updating the cycle), this will not happen. The code below shows the simplest case:

import numpy as np
from matplotlib import pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import time

X = np.random.rand(100, 3)*10
Y = np.random.rand(100, 3)*5

plt.ion()

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.scatter(X[:, 0], X[:, 1], X[:, 2])
plt.draw()

for i in range(0, 20):
    time.sleep(3)   #make changes more apparent/easy to see

    Y = np.random.rand(100, 3)*5
    ax.cla()    
    ax.scatter(Y[:, 0], Y[:, 1], Y[:, 2])
    plt.draw()

Has anyone else encountered this problem?

+3
source share
4 answers

It seems that MaxNoe is right in the sense that the problem is calling ax.cla()or plt.cla(). Actually this seems like a known issue .

, 3D- 3D- (a la sc.set_data(new_values)), ( ).

, , , .

:

datapoints _ofsets3d Line3DCollection, scatter.

:

import numpy as np
from matplotlib import pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import time

X = np.random.rand(100, 3)*10
Y = np.random.rand(100, 3)*5

plt.ion()

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
sc = ax.scatter(X[:, 0], X[:, 1], X[:, 2])
fig.show()

for i in range(0, 20):
    plt.pause(1)

    Y = np.random.rand(100, 3)*5

    sc._offsets3d = (Y[:,0], Y[:,1], Y[:,2])
    plt.draw()
+3

cla():

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

x, y = np.meshgrid(np.linspace(-2,2), np.linspace(-2,2))

ax.plot_surface(x,y, x**2+y**2)
fig.savefig("fig_a.png")

ax.cla()
ax.plot_surface(x,y, x**2+y**2)

fig.savefig("fig_b.png")

: fig_afig_b

+2

, ax.cla(), MaxNoe. , , :

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
fig1 = plt.figure()
ax1 = fig1.add_subplot(111, projection='3d')

x, y = np.meshgrid(np.linspace(-2,2), np.linspace(-2,2))

ax1.plot_surface(x,y, x**2+y**2)
fig1.savefig("fig_a.png")

fig1.clf()
ax1 = fig1.add_subplot(111, projection='3d')
ax1.plot_surface(x,y, x**2+y**2)

fig1.savefig("fig_b.png")
+1

ax = fig.gca(projection='3d') ax = fig.add_subplot(111, projection='3d').

-2

All Articles