更新时间:2022-06-14 23:34:28
有两种方法可以做到这一点
Two ways you could do this would be
set_offsets()
和 set_3d_properties
figure
和/或axes
对象并在每次迭代中绘制一个新的scatter
set_offsets()
and set_3d_properties
figure
and/or axes
object(s) and plot a new scatter
every iteration使用 set_offsets()
和 set_3d_properties
的示例:
Example using set_offsets()
and set_3d_properties
:
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
x = np.linspace(0, np.pi*2, 25)
y = np.sin(x)
z = np.cos(x)
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
def plot(ax):
return ax.scatter(x, y, z)
def update(s):
s.set_offsets(np.stack([x, y], 1))
s.set_3d_properties(z, 'z')
s = plot(ax)
plt.savefig("./before.png")
y = np.cos(x)
z = np.sin(x)
update(s)
plt.savefig("./after.png")
示例清除和重绘:
def plot(fig, ax):
ax.scatter(x, y, z)
plot(fig, ax)
plt.savefig("./before.png")
y = np.cos(x)
z = np.sin(x)
plt.cla()
plot(fig, ax)
plt.savefig("./after.png")
或者,如果您想在同一个散点图中累积每次迭代的数据,您可以将新数据点附加到 x
、y
和 z
对象并使用上述方法之一.
Or, if you want to accumulate the data from every iteration in the same scatter plot, you can just append the new data points to the x
, y
, and z
objects and use one of the above methods.
累积示例:
def plot(ax):
return ax.scatter(x, y, z)
def update(s):
s.set_offsets(np.stack([x, y], 1))
s.set_3d_properties(z, 'z')
s = plot(ax)
plt.savefig("./before.png")
x = np.vstack([x,x])
y = np.vstack([y, np.cos(x)])
z = np.vstack([z, np.sin(x)])
update(s)
plt.savefig("./after.png")
我推荐 set_offsets()
和 set_3d_properties()
的组合.请参见此答案有关确定 figure
和 axes
对象的范围的更多信息.
I would recommend the combination of set_offsets()
and set_3d_properties()
. See this answer for more about scoping the figure
and axes
objects.