Графики и кнопки внутри графиков с Matplotlib (Python)

#python #class #matplotlib #button

#python #класс #matplotlib #кнопка

Вопрос:

Я пытаюсь научить себя, как интегрировать plots и buttons , и с помощью этого расширения, фактически программировать с помощью Classes .

Задача, которую я поставил перед собой, заключалась в:

  1. Создание точечной диаграммы (готово)
  2. Создайте кнопку, которая создает новую точечную диаграмму внутри существующей точечной диаграммы (готово)
  3. Создайте кнопку в новой точечной диаграмме (готово)
  4. Назначьте новой кнопке функциональность закрытия новой точечной диаграммы (застряла здесь)

Это мой прогресс на данный момент:

введите описание изображения здесь

Мой код ниже:

Любые подсказки приветствуются!

 
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