I would like to call a function by pressing a matplotlib-button.
Right now, following approach works:
- Run script
- Press start-button in diagram window => cond=True
- execute function in console by typing: plot_data()
The code looks similar to this,
import matplotlib.pyplot as plt
from matplotlib.widgets import Button
import numpy as np
#---Create BUTTON---
axButton1 = plt.axes([0.1,0.05,0.05,0.05]) #left,bottom, width, height
btn1 = Button(axButton1,"Start")
def start(event):
global cond
cond = True
print(cond)
btn1.on_clicked(start)
#---Setting empty list---
t = np.array([0])
data = np.array([0])
cond = False
#---Plotting REAL TIME data---
def plot_data():
global cond, t, data
if (cond == True):
#SOMETHING WILL BE EXECUTED
However, I would like to execute the function when pressing the start button and not write the command into the console again separately. I tried to call the function "plot_data()" from the function which acess the start-button,
def start(event):
global cond
cond = True
print(cond)
plot_data()
btn1.on_clicked(start)
This however does not work. Any idea what I could try?