最大化 Matplotlib 的大小以填充整个窗口

问题描述 投票:0回答:1

我创建了一个 Python 动画,使用 Matplotlib 图形中的 3D 投影来模拟卫星绕地球运行。我一直在努力解决的这个问题是如何增大图形以使其填满或几乎填满整个图形。如下所示,该图仅占据窗口中央三分之一左右。

我已经尝试了对 Matplotlib 方法的各种调用,如大量在线搜索所示,但尚未找到答案。我的代码的绘图部分在这里。最常见的建议之一是调用ight_layout,但是,虽然情节稍大,但它会将标题推离顶部,所以我将其注释掉。

用于管理大小的 Matplotlib API 非常不透明且令人困惑,所以有人有解决我所要求的技术吗?

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=(5,4)) 
        self.ax = self.fig.add_subplot(1,1,1,projection='3d')
        #self.fig.tight_layout()
        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()

def plot_orbits(self):
        self.dots = []
        for satpos in self.Rpos:
            x,y,z = satpos
            self.ax.plot(x, y, z, color='lightblue', linewidth=0.5)
            dot, = self.ax.plot(x[0:1], y[0:1], z[0:1],
                                marker='o',
                                markersize=6,
                                color='black')
            self.dots.append(dot)

        self.hour_text = self.ax.text2D(0.25, 0.15, "", 
                                   transform=self.ax.transAxes,
                                   color='green',
                                   fontsize=12)
python matplotlib
1个回答
0
投票

matplotlib 中的 3d 绘图的微调非常棘手...我经常使用一个技巧来修复空白:

fig, tmp_ax = plt.subplots(figsize=(7, 4)) 
ax = fig.add_axes([0.05, 0.05, 0.95, 0.95], projection='3d')  # (left, bottom, width, height)
tmp_ax.set_axis_off()  # hide axis

# and from now on only use the axis `ax`, e.g., 
# ax.scatter(x, y, z)

add_axes
函数创建一个新轴并将其定位在图形上。因此,您可以确保整个图都是可见的。需要进行一些反复试验才能找出
add_axes
的最佳值。但这似乎是最可靠的方法 AFAIK。

© www.soinside.com 2019 - 2024. All rights reserved.