rbaltrusch / bach_generator

A Bach music generator using a neural network trained using a genetic algorithm.
MIT License
5 stars 1 forks source link

Add 'clear plot' button #56

Open rbaltrusch opened 1 week ago

rbaltrusch commented 1 week ago

Add 'clear plot' button. Currently, the only way to clear plots of previous runs is to restart the GUI.

ShubhamKalsekar commented 1 week ago

Install pip install matplotlib ipywidgets

Create a Plot with a "Clear Plot" Button

import matplotlib.pyplot as plt from ipywidgets import Button, VBox from IPython.display import display

fig, ax = plt.subplots() #figure and axis

x = [1, 2, 3, 4, 5] #some initial plot data y = [1, 4, 2, 3, 5] line, = ax.plot(x, y)

def clear_plot(b): ax.cla() # Clearing the current axis fig.canvas.draw() # Redraw the figure

clear_button = Button(description="Clear Plot") #Create a button and set the function to on click clear_button.on_click(clear_plot)

display(VBox([clear_button, fig.canvas])) #showing the button

Reference https://www.activestate.com/resources/quick-reads/how-to-clear-a-plot-in-python/

rbaltrusch commented 5 days ago

Hi @ShubhamKalsekar, thanks for your contribution. Can you integrate the code for this button into the existing gui and send a PR my way? Thanks :+1: Let me know if you need any help :)

PS: I would prefer not to add any extra dependencies to the project, I believe the current dependencies matplotlib and tkinter should suffice for this.