我有一个 matplotlib 图,我想根据用户输入进行更新。用户为三个参数输入三个值,并根据这三个输入构建一条路径。然后,该算法获取该路径目录中的每个图像,过度混合所有图像并显示它们。
该算法通常运行良好(无需用户输入),但现在,绘图似乎永远不会更新。然而,路径是根据输入正确构建的。我通过每次单击更新按钮时打印新路径来检查这一点。
这是我的代码:
import matplotlib.pyplot as plt
import ipywidgets as widgets
from IPython.display import display
import numpy as np
# Function to update the plot based on button clicks
def update_plot(param1, param2, param3):
# Construct the path based on the selected values and the image filename based on slider
positions
path = f'C:/Users/Jeff/Desktop/images/{param1}mL VE-Wasser + {param2} L Stickstoff +{param3} W'
new_path = path + '/*.JPG'
# Create list to later save the individual segmented images
segmented_imgs = []
for image in glob.glob(new_path):
# Add all segmented images to a list of images (their numpy arrays)
segmented_img = prepare_images()
segmented_imgs.append(segmented_img)
# Get the overblended final image
overblended_img = sum(segmented_imgs)
# Divide pixel intesities by number of images to get back to scale 0 to 1
overblended_img = overblended_img / len(segmented_imgs)
# Plot spraying cone with different regions integrated over the image series (overblended)
plt.figure(figsize=(10, 12))
plt.imshow(overblended_img)
# Create interactive widgets for each parameter
param1_widget = widgets.Text(value='', description='Medium in mL/min:')
param2_widget = widgets.Text(value='', description='Gas in L/min:')
param3_widget = widgets.Text(value='', description='Leistung in W:')
# Create a button to update the plot update_button = widgets.Button(description="Update Plot")
# Define the callback function for the button
def on_button_clicked(b):
update_plot(param1_widget.value, param2_widget.value, param3_widget.value)
update_button.on_click(on_button_clicked)
# Display the widgets and button
display(param1_widget, param2_widget, param3_widget, update_button)
我不能确定,因为问题中的示例不可重现,但似乎每次更新绘图时您都会创建一个新图形。将行
plt.figure(figsize=(10, 12))
移到 update_plot
函数之外,仅创建一次。
import matplotlib.pyplot as plt
import ipywidgets as widgets
from IPython.display import display
import numpy as np
# Function to update the plot based on button clicks
def update_plot(param1, param2, param3):
# Construct the path based on the selected values and the image filename based on slider
positions
path = f'C:/Users/Jeff/Desktop/images/{param1}mL VE-Wasser + {param2} L Stickstoff +{param3} W'
new_path = path + '/*.JPG'
# Create list to later save the individual segmented images
segmented_imgs = []
for image in glob.glob(new_path):
# Add all segmented images to a list of images (their numpy arrays)
segmented_img = prepare_images()
segmented_imgs.append(segmented_img)
# Get the overblended final image
overblended_img = sum(segmented_imgs)
# Divide pixel intesities by number of images to get back to scale 0 to 1
overblended_img = overblended_img / len(segmented_imgs)
# Plot spraying cone with different regions integrated over the image series (overblended)
# CHANGED - Don't create a new figure here
plt.imshow(overblended_img)
# Create interactive widgets for each parameter
param1_widget = widgets.Text(value='', description='Medium in mL/min:')
param2_widget = widgets.Text(value='', description='Gas in L/min:')
param3_widget = widgets.Text(value='', description='Leistung in W:')
# Create a button to update the plot update_button = widgets.Button(description="Update Plot")
# Define the callback function for the button
def on_button_clicked(b):
update_plot(param1_widget.value, param2_widget.value, param3_widget.value)
update_button.on_click(on_button_clicked)
# CHANGED - Create the figure only once e.g. down here
fig = plt.figure(figsize=(10, 12))
# Display the widgets and button
display(param1_widget, param2_widget, param3_widget, update_button)