Why does the input mutation of the matplotlib Axes3D.plot () and .scatter () methods behave differently?

In the code posted in the question How can I draw a multiple image of three-dimensional curves using Python? , the plot method is called twice, and since the points on the graph are not reinstalled, the lines sink on top of the other. But instead plot(), if we try to use the scatter method, we can see points built elsewhere. Why is this a change in behavior?

Code copied below

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


t=2  #t can be changed

fig = plt.figure()
ax=Axes3D(fig)

#data

def unitilize(x,y,z):
    r=mt.sqrt(x**2+y**2+z**2)
    return x/r, y/r, z/r

def g_1(x,y,z):
    x=t*x                
    z=z/t                
    x,y,z=unitilize(x,y,z)
    return x,y,z

stepCnt=10000            ######step 
#########data#################
xs = np.empty((stepCnt + 1,))
ys = np.empty((stepCnt + 1,))
zs = np.empty((stepCnt + 1,))

#Setting initial values
def huatu(x,y,z):   

    xs[0], ys[0], zs[0] =unitilize(x,y,z)

    for i in range(stepCnt):
        xs[i+1],ys[i+1],zs[i+1]=g_1(xs[i], ys[i], zs[i])
    return xs,ys,zs


xs3,ys3,zs3=huatu(1,10,40)
ax.plot(xs3, ys3, zs3, color='b', marker='x')

xs2,ys2,zs2=huatu(1,0,40)
ax.plot(xs2, ys2, zs2, color='r', marker='o')
plt.show()

Chart Output: enter image description here

Scattering Output: enter image description here

+4
source share
1 answer

, - , , . , , Axes3D.plot ( Axes.plot, ) , . , , . - Axes.plot, , . - , Axes3D, .

, Axes3D.scatter PathCollection (cast to PathCollection3D), . , ( 2d) ._offsets, ndarray, . .

plot, , . :

import numpy as np
import matplotlib.pyplot as plt

fig,ax = plt.subplots()

# first set data to zero
# we'll use an ndarray as input, otherwise there no chance to get a view
x = np.arange(3)
y = np.array([0.0,0.0,0.0])

# plot the flat line
pl, = ax.plot(x,y,'o-')

# change the axes for better comparison later; not actually relevant
ax.set_ylim([0,4])

# see that the input data are kept as views
print(pl.get_xdata().base is x)  # True
print(pl.get_ydata().base is y)  # True

# mutating x would actually change pl.get_xdata() and vice versa

# mutate y to get a nontrivial line
y[:] = [1,2,3]

# update the canvas in an interactive plot
# plt.show() probably suffices non-interactively
fig.canvas.draw()

plt.show()

:

2d plot case

, print , , , plot, ( ) , .

:

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')

# first set data to zero
# we'll use an ndarray as input, otherwise there no chance to get a view
x = np.arange(3)
y = np.array([0.0,0.0,0.0])
z = np.array([0.0,0.0,0.0])

# plot the flat line
pl, = ax.plot(x,y,z,'o-')

# change the axes to see the result; not actually relevant
ax.set_ylim([0,4])
ax.set_zlim([0,4])

# mutate y,z to get a nontrivial line
y[:] = [1,2,3]
z[:] = [1,2,3]


# update the canvas in an interactive plot
# plt.show() probably suffices non-interactively
fig.canvas.draw()

plt.show()

3D- ( ), :

3d case

, , 2- .

, ; Axes3D.plot outsources Axes.plot (, 2d ), . Axes.plot, , .

Axes3D.scatter Axes.scatter 2d-. , plot 2d 3d, : PathCollection(3D) .

, , , ( ) xs,ys,zs. , , , , . Axes3D.plot , , , ; Axes3D.scatter , , .


, 3d- ndarray:

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')

# first set data to zero, but lists this time
x = np.arange(3)
y = [0.0,0.0,0.0]
z = [0.0,0.0,0.0]

# plot the flat line
pl, = ax.plot(x,y,z,'o-')

# change the axes to see the result; not actually relevant
ax.set_ylim([0,4])
ax.set_zlim([0,4])

# mutate y,z to get a nontrivial line
y[:] = [1,2,3]
z[:] = [1,2,3]


# update the canvas in an interactive plot
# plt.show() probably suffices non-interactively
fig.canvas.draw()

plt.show()

, ndarrays, , , , . :

3d case using list input

-, y , z . ! :

print(pl._verts3d)
# (array([0, 1, 2]), array([ 0.,  0.,  0.]), [1, 2, 3])
print(pl._verts3d[2] is z)
# True

Axes3D.plot z , mplot3d.art3d.line_2d_to_3d, x y 2d z .

, Axes.plot y , y . , z , , , . y, z, z.


matplotlib 2d. , , 2d- , . , 3d- -, .

, , . , , pl.set_xdata(). , ( x/ydata ). , , . , , matplotlib . , . - , , , , , , , .

+2

Source: https://habr.com/ru/post/1681808/


All Articles