Кнопка Matplotlib не отвечаетPython

Программы на Python
Anonymous
Кнопка Matplotlib не отвечает

Сообщение Anonymous »

Я хочу добавить кнопку, которая позволит пользователю переключать один из четырех графиков между двумя разными наборами данных, однако кнопка не реагирует.
Кнопка должна вызывать функцию функция, которая должна обновлять сюжет... Я действительно ломаю голову, почему это не так. Может ли кто-нибудь пролить свет на этот вопрос?
Вот минимальный воспроизводимый пример:
import matplotlib.pyplot as plt
import matplotlib.backends.backend_tkagg as tkagg
import tkinter as tk
from matplotlib.widgets import Button

def qc_manual(filtered_data, figure, canvas):

def update_plot_colors(axes, lines_to_analyze):
for ax in axes.flat:
for line in ax.lines:
analysis = lines_to_analyze.get(line)
line.set_color(
'blue' if analysis.active and analysis.analysis_type == 'lineblank' else
'gray' # inactive
)
line.set_marker('o' if analysis.active else 'x')

# toggle the 2/40 amu data
def on_click(event, filtered_data, axes, lines_to_analyze, canvas):

# clear the lines and axes from the bottom right plot
axes[1, 1].clear()
for line in list(lines_to_analyze.keys()):
if line in axes[1, 1].lines:
del lines_to_analyze[line]

title = axes[1, 1].get_title()
new_title = '2 amu - All Data' if '40 amu - All Data' in title else '40 amu - All Data'
axes[1,1].set_title(new_title)

for analysis in filtered_data:

# filter data for active indices
mass = '2 amu' if new_title == '2 amu - All Data' else '40 amu'
x = analysis.time_sec
y = analysis.raw_data[mass]
line_color = 'blue' if analysis.active else 'gray'
line_marker = 'o' if analysis.active else 'x'
if new_title == '2 amu - All Data':
line, = axes[1,1].plot(x, y, marker=line_marker, linestyle='-', picker=5, color=line_color, zorder=2)
else:
line, = axes[1,1].plot(x, y, marker=line_marker, linestyle='-', picker=5, color=line_color, zorder=2)

lines_to_analyze[line] = analysis

# recreate the button
toggle_button_ax = axes[1, 1].inset_axes([0.75, 1, 0.25, 0.1])
figure.toggle_button = Button(toggle_button_ax, 'Toggle 2/40 amu')
figure.toggle_button.on_clicked(lambda event: on_click(event, filtered_data, axes, lines_to_analyze, canvas))

# formatting
axes[1,1].set_title(new_title)
update_plot_colors(axes, lines_to_analyze)
canvas.draw()

lines_to_analyze = {} # dictionary to map lines to analyses for efficient lookup

for ax, (mass, title) in zip(axes.flat, [
('4 amu', '4 amu - Gas Standards'),
('4 amu', '4 amu - Blanks'),
('3 amu', '3 amu - All Data'),
('40 amu', '40 amu - All Data')
]):
for analysis in filtered_data:

# formatting
line_color = 'blue' if analysis.active else 'gray'
line_marker = 'o' if analysis.active else 'x'

x = analysis.time_sec
y = analysis.raw_data[mass]

# draw the data
line, = ax.plot(x, y, marker=line_marker, linestyle='-', picker=5, color=line_color, zorder=2)

# store the line and analysis in the dictionary
lines_to_analyze[line] = analysis

# formatting
ax.set_title(title)

# create a button to toggle between 40 amu and 2 amu
toggle_button_ax = axes[1, 1].inset_axes([0.75, 1, 0.25, 0.1])
figure.toggle_button = Button(toggle_button_ax, 'Toggle 2/40 amu')
figure.toggle_button.on_clicked(lambda event: on_click(event, filtered_data, axes, lines_to_analyze, canvas))

# update the plot
update_plot_colors(axes, lines_to_analyze)

plt.tight_layout()
canvas.draw()

filtered_data_list = []

for i in range(5): # replace 5 with the number of analyses you want
filtered_data = type('filtered_data', (object,), {'raw_data': {}, 'time_sec': [], 'analysis_type': 'lineblank', 'active': True})
filtered_data.time_sec = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100]
filtered_data.raw_data['4 amu'] = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
filtered_data.raw_data['2 amu'] = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
filtered_data.raw_data['3 amu'] = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
filtered_data.raw_data['40 amu'] = [2, 4, 6, 8, 10, 12, 14, 16, 18, 20]
filtered_data_list.append(filtered_data)

# setup the main window, figure, and canvas
root = tk.Tk()
figure = plt.figure()
figure.set_size_inches(12, 8)
axes = figure.subplots(2, 2)
canvas = tkagg.FigureCanvasTkAgg(figure, master=root)
canvas.get_tk_widget().pack(side=tk.TOP, fill=tk.BOTH, expand=True)
canvas.draw()
qc_manual(filtered_data_list, figure, canvas)
root.mainloop()



Подробнее здесь: https://stackoverflow.com/questions/784 ... responding

Вернуться в «Python»