在Jupyter Python笔记本中清除MatPlotLib图

时间:2017-03-24 11:10:59

标签: python matplotlib jupyter-notebook ipywidgets

我想在MatplotLib中的3D散点图在Jupyter Python笔记本中以交互方式旋转。出于这个原因,我集成了ipywidgets的滑块来更新视角。下面的测试代码显示了我想要实现的目标。问题是在前一个数字下方添加了一个新数字,而不是清除当前数字。我尝试plt.close(fig)plt.cla()plt.clf()但没有成功。 (我进一步意识到重新创建图形和轴有开销,但这是我目前关注的较小部分......)

这是(测试)代码:

# 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()

任何建议都将不胜感激!

1 个答案:

答案 0 :(得分:6)

您称之为开销是问题的根源。或者换句话说:如果在每次调用函数时,都会创建一个新的数字,那么您最终会得到很多数据会令人惊讶吗?

这个想法当然是画一个人物。为了能够稍后更新数字,需要%matplotlib notebook后端。

更改滑块时调用的函数只需要更新视角并重绘画布。

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import ipywidgets as widgets
from IPython.display import display
%matplotlib notebook

# generate test data
x = np.random.rand(100)
y = np.random.rand(100)
z = np.random.rand(100)


fig = plt.figure(figsize=(6,6))
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)
ax.view_init(20, 40)
# show plot
plt.show()

def update_plot(angle1 = 20, angle2 = 40):
    # set view angle
    ax.view_init(angle1, angle2)
    fig.canvas.draw_idle()

# 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):
    update_plot(angle1 = value['new'])
    angle1_label.value = 'Angle 1 value is: ' + str(value.new)

angle1_slider.observe(update_angle1, names = 'value')

这就是它的样子:

enter image description here