我有一个三维坐标数据集在时间上游动。所以我的ndarray数据如下:
points = [ # 1st dim: Time-step (/timeframe)
[ # 2nd dim: Different Objects
[ # 3rd dim: The coordinates of the Objects
2.115, 2.387, 3.3680003],
[2.059, 2.302, 3.2610002],
[2.1720002 2.2280002 3.1880002]],
[[1.9530001, 2.306, 3.5760002],
[1.9510001, 2.265, 3.433 ],
[2.1000001, 2.2440002, 3.381]],
[[1.6760001, 2.459, 3.4650002],
[1.5710001, 2.434, 3.367 ],
[1.6320001 2.4320002 3.2250001]]]我正在一个接一个地绘制这些物体:
import matplotlib.pyplot as plt
for timeframe in range(points.shape[0]):
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
x_coordinates = points[timeframe][:, 0]
y_coordinates = points[timeframe][:, 1]
z_coordinates = points[timeframe][:, 2]
ax.scatter(x_coordinates, y_coordinates, z_coordinates, c='r', marker='o')
plt.show()现在我想制作一个图或一个窗口,在那里我可以使用一个滑块来根据时间步骤在所绘制的图形之间进行选择。
是否有代码或python模块,用于在for循环中选择一个图,例如通过滑块?
发布于 2022-06-16 14:24:16
是的,您可以使用matplotlib小部件库,但为此您需要进行matplotlib交互。
您可以添加一个水平滑块(参见示例:https://matplotlib.org/stable/gallery/widgets/slider_demo.html),并在选择和移动滑块时调用一个函数。在我提供的示例中,该函数称为update。当通过移动滑块值来更新滑块值时,它将调用此函数并将实际的滑块值提供给它。
因此,您可以创建一个带有for loop整数值的滑块,在更新滑块时,调用update函数,并在其中使用来自滑块的整数值绘制数据的代码。
滑翔机
axfreq = plt.axes([0.25, 0.1, 0.65, 0.03])
freq_slider = Slider(
ax=axfreq,
label='Time Steps',
valmin=0, # minimun value of range
valmax=points.shape[0] - 1, # maximum value of range
valinit=0,
valstep=1.0 # step between values
)当你看到我分享的例子时,我几乎所有的名字都能帮上忙。请注意,当您滑动滑块时,我添加了valstep=1.0以增加1。
更新函数
def update(val)
val = int(val) # it must be an integer
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
x_coordinates = points[val][:, 0]
y_coordinates = points[val][:, 1]
z_coordinates = points[val][:, 2]
ax.scatter(x_coordinates, y_coordinates, z_coordinates, c='r', marker='o')
plt.show()这可能会引起一些问题,因为您可能没有matplotlib交互。如果您使用的是jupyter-记事本非常容易让它具有交互性,您只需在顶部添加这一行:
%matplotlib widget如果它不起作用,你可以试试:
%matplotlib notebook您还可能需要安装一些辅助模块。
如果您希望它恢复正常,只需在顶部再添加一次:
%matplotlib inline关于matplotlib中交互式情节的更多信息:https://matplotlib.org/stable/users/explain/interactive.html
https://stackoverflow.com/questions/72646964
复制相似问题