When I adjust the position slider, for example pdfReader X,Y,Z, the point(pdfReader) in the plot won't be updated accordingly. Instead, I would see <Figure size 640x480 with 0 Axes> in my jupyter output block
Here is the python code I tried.
import ipywidgets as widgets
from IPython.display import display
# Initial data
dtype_ = np.dtype('float32')
element_positions = np.array([
np.array([0.36918038, 0.12599993, 0.5699662], dtype=dtype_),
np.array([0.409, -0.03100002, 0.531], dtype=dtype_)
])
element_labels = ['MusicPlayer', 'PDFReader']
# Create a 3D scatter plot
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
# Create a function to update and redraw the plot
def update_plot():
ax.clear()
ax.scatter(element_positions[:, 0], element_positions[:, 1], element_positions[:, 2], c='r', marker='^')
for i, label in enumerate(element_labels):
ax.text(element_positions[i, 0], element_positions[i, 1], element_positions[i, 2], label, fontsize=8)
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
ax.set_title('3D Scatter Plot')
plt.draw()
# Create sliders for updating element positions
sliders = []
for i, label in enumerate(element_labels):
sliders.append(widgets.FloatSlider(value=element_positions[i, 0], min=-1, max=1, step=0.01, description=f'{label} X'))
sliders.append(widgets.FloatSlider(value=element_positions[i, 1], min=-1, max=1, step=0.01, description=f'{label} Y'))
sliders.append(widgets.FloatSlider(value=element_positions[i, 2], min=-1, max=1, step=0.01, description=f'{label} Z'))
def update_positions(change):
for i in range(len(sliders)):
element_positions[i // 3, i % 3] = sliders[i].value
update_plot()
for i in range(len(sliders)):
sliders[i].observe(update_positions, names='value')
# Display the sliders and the plot
slider_widgets = widgets.VBox(sliders)
display(widgets.HBox([slider_widgets]))
update_plot()
plt.show()
what you need to do is to switch to an interactive matplotlib backend for jupyter notebooks! The recommended one here would be ipympl.
Once installed, all you need to do is to activate the backend with the magic-command (at the very top of your script)
and then all should work as expected!