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

Jupyter中matplotlib widget工具栏水平放置及坐标指示器调整咨询

两个需求均可实现,修改后的完整可运行代码如下:

%matplotlib widget
import matplotlib.pyplot as plt
from ipywidgets import widgets, Layout
from IPython.display import display

out_widget_plot = widgets.Output()
with out_widget_plot:
    with plt.style.context("default"):
        fig, ax1 = plt.subplots(1, 1)
        # 隐藏默认的Figure 1标题栏
        fig.canvas.header_visible = False
        # 隐藏默认底部的状态栏容器
        fig.canvas.footer_visible = False

fig.suptitle('test title', fontsize=10) 
tline = ax1.plot([0,1,2], [10,20,30])
fig.tight_layout()

# 提取工具栏、坐标指示器组件
toolbar = fig.canvas.toolbar
coordinate_indicator = fig.canvas.toolbar.status_bar

# 设置工具栏为水平布局
toolbar.layout = Layout(display='flex', flex_flow='row', justify_content='flex-start', width='100%')

# 组装绘图区域的新布局
plot_container = widgets.VBox([
    coordinate_indicator,
    toolbar,
    fig.canvas
])

the_ui = widgets.VBox([
    widgets.HTML("<b>Hello</b>"),
    plot_container,
    widgets.HTML("<b>World</b>"),
])
display(the_ui)
实现逻辑说明
  • 隐藏"Figure 1"默认标题:通过fig.canvas.header_visible = False直接关闭画布自带的头部标题栏,不会影响自定义的suptitle标题显示
  • 工具栏调整:
    • 调用fig.canvas.toolbar即可获取默认的垂直工具栏组件
    • 给工具栏设置flex_flow='row'的布局属性即可切换为水平排列
    • 将工具栏从原侧边位置取出,放到绘图区域上方的布局容器中即可
  • 坐标指示器调整:
    • 底部的坐标指示器对应组件为fig.canvas.toolbar.status_bar
    • 隐藏原有底部状态栏容器后,将该组件单独取出放在工具栏上方即可

内容的提问来源于stack exchange,提问作者sdbbs

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.05 00:42:03