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

如何让Matplotlib 3D绘图填满整个窗口?

解决Matplotlib 3D卫星模拟动画填不满窗口的问题

方法1:手动调整画布尺寸与子图边距

当前画布尺寸(5,4)过小,默认边距会造成大量留白。可以放大画布,并手动将子图边距设为0,让绘图完全占满窗口:

def fly(self):
    hours = np.arange(0,24,0.01)
    now = GPSOrbitalSimulator.ts.now().utc
    t = GPSOrbitalSimulator.ts.utc(now.year, now.month, now.day, hours)

    self.fig = plt.figure(figsize=(10, 8))  # 增大画布尺寸,可根据屏幕调整
    self.ax = self.fig.add_subplot(1,1,1,projection='3d')
    # 手动消除子图边距,让绘图占满整个画布
    self.fig.subplots_adjust(left=0, right=1, bottom=0, top=1, wspace=0, hspace=0)
    self.ax.grid(False)
    self.ax.set_axis_off()
    self.draw_earth()

    self.compute_orbits(t)
    self.plot_orbits()

    ani = FuncAnimation(self.fig,
                        lambda x: self.update(x, hours),
                        frames=range(len(hours)),
                        repeat=True,
                        interval = 30
                        )

    plt.show()

方法2:使用constrained_layout智能管理布局

替代tight_layout,constrained_layout会自动调整元素位置,避免标题被推出窗口,同时最大化绘图区域:

def fly(self):
    hours = np.arange(0,24,0.01)
    now = GPSOrbitalSimulator.ts.now().utc
    t = GPSOrbitalSimulator.ts.utc(now.year, now.month, now.day, hours)

    # 开启constrained_layout,同时放大画布
    self.fig = plt.figure(figsize=(10, 8), constrained_layout=True)
    self.ax = self.fig.add_subplot(1,1,1,projection='3d')
    self.ax.grid(False)
    self.ax.set_axis_off()
    self.draw_earth()

    self.compute_orbits(t)
    self.plot_orbits()

    ani = FuncAnimation(self.fig,
                        lambda x: self.update(x, hours),
                        frames=range(len(hours)),
                        repeat=True,
                        interval = 30
                        )

    plt.show()

方法3:适配3D坐标轴范围

如果内容本身在3D空间中占比小,可自动或手动调整坐标轴范围,让地球和卫星轨道填满3D视图:
在self.draw_earth()之后添加:

# 自动缩放坐标轴以适配所有绘图内容
self.ax.autoscale_view()

# 若自动缩放效果不佳,可手动设置范围(根据你的轨道实际尺寸调整)
# self.ax.set_xlim(-26500, 26500)
# self.ax.set_ylim(-26500, 26500)
# self.ax.set_zlim(-26500, 26500)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.25 20:21:01