使用Matplotlib(Python)的绘图和绘图中的按钮

使用Matplotlib(Python)的绘图和绘图中的按钮,python,class,matplotlib,button,Python,Class,Matplotlib,Button,我试着教自己如何集成绘图和按钮,并通过扩展,实际使用类编程 我给自己设定的挑战是: 创建散点图(完成) 创建在现有散点图中生成新散点图的按钮(完成) 在新散点图中创建一个按钮(完成) 为“新建”按钮指定关闭新散点图的功能(卡在此处) 这是我目前的进展: 我的代码如下: 欢迎任何提示 import matplotlib.pyplot as plt from matplotlib.widgets import Button # Junk data for the purpose of m

我试着教自己如何集成
绘图
按钮
,并通过扩展,实际使用
编程

我给自己设定的挑战是:

  • 创建散点图(完成)
  • 创建在现有散点图中生成新散点图的按钮(完成)
  • 在新散点图中创建一个按钮(完成)
  • 为“新建”按钮指定关闭新散点图的功能(卡在此处)
  • 这是我目前的进展:

    我的代码如下:

    欢迎任何提示

    
    import matplotlib.pyplot as plt
    from matplotlib.widgets import Button
    
    
      # Junk data for the purpose of my question
    
    x = [1, 1, 1, 2, 2, 2, 3, 3, 3]
    y = [1, 2, 3, 1, 2, 3, 1, 2, 3]
    
    fig, (ax1) = plt.subplots()
    
    ax1.scatter(x, y)  # plot (x, y) data as a scatter plot
    
    class Initial_plot(object):
    
        def close_plot(self, event):
            plt.close()
    
        def add_axis(self, event):
            plt.axes([0.3, 0.2, 0.5, 0.51])
    
            x = [1, 1, 1, 2, 2, 2, 3, 3, 3]
            y = [1, 2, 3, 1, 2, 3, 1, 2, 3]
    
            plt.scatter(x, y)
    
            class Second_plot(object):
    
                def close_addplot(self, event):
                    plt.close()
    
    
              # Button to close the new scatter plot
            axclose_addplot = plt.axes([0.4, 0.4, 0.15, 0.1])
            bclose_addplot = Button(axclose_addplot, 'Close add plot')
            bclose_addplot.on_clicked(callback.close_addplot)
    
    
    callback = Initial_plot()
    
    
      # Add button for a new scatter plot
    
    axadd_plot = plt.axes([0.5, 0.01, 0.1, 0.071])
    badd_plot = Button(axadd_plot, 'Add Plot')
    badd_plot.on_clicked(callback.add_axis)
    
    
    
      # Exit Button for inital scatter plot
    axclose_plot = plt.axes([0.7, 0.01, 0.1, 0.071])  # co-ords of button
    bclose_plot = Button(axclose_plot, 'Close')  # name of button
    bclose_plot.on_clicked(callback.close_plot)  # enables event upon clicking the button
    
    plt.show()  #display the plot