You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Jupyter Notebook中使用%matplotlib widget时图表内容重复且控件对齐依赖HBox的问题求助

Jupyter Notebook中使用%matplotlib widget时图表内容重复且控件对齐依赖HBox的问题求助

各位好,我在Jupyter Notebook里用%matplotlib widget做交互式绘图,目前交互功能能正常工作,但遇到了两个头疼的问题:

  • 绘图内容出现了重复显示的情况
  • 必须依赖HBox来对齐控件,一旦去掉display函数,虽然图表能渲染,但控件和图表完全错位,根本没法正常使用

以下是我的完整代码,麻烦大家帮忙排查下问题所在:

%matplotlib widget
import tomolyzer as t
import numpy as np
import matplotlib.pyplot as plt
from ipywidgets import interact, IntSlider, FloatSlider, Dropdown, VBox, HBox, Layout
from IPython.display import display  # Import display for rendering widgets
import logging
from scipy.fft import fft2, fftshift
import scipy.ndimage as S
import os
from tqdm import tqdm
fig, axs = plt.subplots(1, 2, figsize=(10, 5))  # 1 row and 2 columns, figure size of 10 inches wide and 5 inches tall.

axs[0].axis("off")
axs[1].axis("off")

std = 0
max = 0
factor = max - std*2
i=0
image = data[i][testing_radius, :, :].copy()
axs[0].imshow(image, cmap='viridis', aspect='auto')  # Display the slice
axs[0].set_title(f'{body.centers_scaled[title][i]}')  # Title for the slice
axs[0].set_xlabel(f'{np.mean(image):.2e}')
axs[0].set_ylabel('YX')
axs[0].set_xticks([0,mid_x/2,mid_x])
axs[0].set_yticks([0,mid_y/2,mid_y])

axs[1].imshow(image, cmap='viridis', aspect='auto')  # Display the slice

def updatePlot(val):
    min = np.min(data[i][18])
    max = np.max(data[i][18])
    mean = np.mean(data[i][18])
    std = np.std(data[i][18])
    threshold= max - std*val
    image = data[i][testing_radius, :, :].copy()
     # positive intercept
    image[np.where(image > max - std*val)] = 0
    image[np.where(image < max - std*val)] = 1
    axs[1].clear()  # Clear previous plot
    axs[1].imshow(image, cmap='viridis', aspect='auto')  # Display the slice
    axs[1].text(
        0.5, -0.1, f"{threshold:.2e}", 
        transform=axs[1].transAxes, ha='center', va='top', fontsize=12
    )

std_slider = FloatSlider(min=0,max=7,step=0.001,value=0,description='Std',orientation='vertical', layout=Layout(height='300px'))
x=interact(updatePlot,val = std_slider)

plots_and_slider = HBox([std_slider,fig.canvas], layout=Layout(align_items='center'))

display(plots_and_slider)

备注:内容来源于stack exchange,提问作者n8CodeGuru

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.14 13:15:29