I want a 3D scatter plot in MatPlotLib to be rotated interactively in a Jupyter Python notebook. For that reason I integrated a slider from ipywidgets
to update the view angle. The test code below shows what I am trying to achieve. The problem is that a new figure is added below the previous one, instead of the current figure cleared. I tried plt.close(fig)
, plt.cla()
and plt.clf()
without success. (further I realize that there is overhead in recreating the figure and axes but that is the lesser part of my current concerns...)
Here is the (test) code:
# init
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import ipywidgets as widgets
from IPython.display import display
# generate test data
x = np.random.rand(100)
y = np.random.rand(100)
z = np.random.rand(100)
# prepare plot
def draw_plot(angle1 = 20, angle2 = 40):
# create figure
fig = plt.figure(figsize=(15,10))
ax = fig.add_subplot(111, projection='3d')
ax.set_xlabel('X axis')
ax.set_ylabel('Y axis')
ax.set_zlabel('Z axis')
ax.scatter(x, y, z)
# set view angle
ax.view_init(angle1, angle2)
# show plot
plt.show()
# prepare widgets
angle1_slider = widgets.IntSlider(20, min = 0, max = 60)
angle1_label = widgets.Label(value = 'Angle 1 value is: ' + str(angle1_slider.value))
display(angle1_slider, angle1_label)
# handle angle 1 update
def update_angle1(value):
draw_plot(angle1 = value['new'])
angle1_label.value = 'Angle 1 value is: ' + str(value.new)
angle1_slider.observe(update_angle1, names = 'value')
# draw initial plot
draw_plot()
Any suggestions would be appreciated!