如何删除绘图和窗口之间的填充

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

我正在尝试在 3D 坐标系中重建太阳系。我构建了一个球体作为太阳,并围绕它构建了多个圆圈(轨道),其中球体作为行星。但我不知道如何删除绘图和窗口之间的填充,以便您可以看到轨道的右侧和左侧。

图片 - 太阳系,但外轨道被左右切掉

`

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

scale_distance = 1/10000000
scale_sphere = 1/2

fig, ax = None

class tool():
    def __init__(self, fig, ax):
        self.fig = fig
        self.ax = ax

    def create_fig(self, facecolor, figsize=(16, 9)):
        self.fig = plt.figure(facecolor=facecolor, figsize=figsize)
        self.ax = self.fig.add_subplot(111, projection='3d')
        self.ax.set_facecolor(facecolor)
        return self.fig, self.ax    
    
    def set_axis_off(self):
        self.ax.grid(False)
        self.ax.set_xticks([])
        self.ax.set_yticks([])
        self.ax.set_zticks([])
        self.ax.set_xlabel('')
        self.ax.set_ylabel('')
        self.ax.set_zlabel('')
        self.ax.set_axis_off()
        return self.ax

    def set_axis_limits(self, size_koordsystem):
        self.ax.set_box_aspect([1, 1, 1])
        self.ax.set_xlim([size_koordsystem, size_koordsystem])
        self.ax.set_ylim([size_koordsystem, size_koordsystem])
        self.ax.set_zlim([size_koordsystem,size_koordsystem])
        return self.ax
    

class sphere():
    def __init__(self, u, v):
        self.u = u
        self.v = v

    def values_for_sphere(self):
        x = np.outer(np.cos(self.u), np.sin(self.v))
        y = np.outer(np.sin(self.u), np.sin(self.v))
        z = np.outer(np.ones(np.size(self.u)), np.cos(self.v))
        return x, y, z
    
    def plot_sphere(self, ax, rs, cr, color, alpha, x_offset=0, y_offset=0, z_offset=0):
        x, y, z = self.values_for_sphere()
        ax.plot_surface(x+x_offset, y+y_offset, z+z_offset, rstride=rs, cstride=cr, color=color, alpha=alpha)
        return ax


class orbit():
    def __init__(self, a_planet):
        self.a_planet = a_planet

    def values_for_orbit(self, a_planet, e):
        if a_planet is None:
            raise ValueError("Der Planetenradius darf nicht None sein.")
        if not (0 <= e < 1):
            raise ValueError("Die Exzentrizität muss zwischen 0 und 1 liegen.")
        a_planet = a_planet * scale_distance
        a = np.linspace(0, 2 * np.pi, 100)
        x_orbit = a_planet * np.cos(a)
        y_orbit = a_planet * np.sqrt(1 - e**2) * np.sin(a)
        return x_orbit, y_orbit

    def plot_orbit(self, ax, a_planet, e, color, alpha):
        x_orbit, y_orbit = self.values_for_orbit(a_planet, e)
        ax.plot(x_orbit, y_orbit, 0, color=color, alpha=alpha)
        return ax

planets = [
    ["Merkur", 57.90, 0.2056, 0.24, 3.3011e23, 2.439, 7*np.pi/180],
    ["Venus", 108.21e6, 0.0067, 0.62, 4.8675e24, 6.052, 3.39*np.pi/180],
    ["Erde", 146.6e6, 0.0167, 1.00, 5.97237e24,6.371, 0],
    ["Mars", 227.9e6, 0.0934, 1.88, 6.4171e23, 3.389, 1.85*np.pi/180],
    ["Jupiter", 778.57e6, 0.0483, 11.86, 1.8982e27, 69.911, 1.305*np.pi/180],
    ["Saturn", 1433.53e6, 0.0541, 29.46, 5.6834e26, 58.232, 2.485*np.pi/180],
    ["Uranus", 2872.46e6, 0.0471, 84.01, 8.6810e25, 25.362, 0.772*np.pi/180],
    ["Neptun", 4497.06e6, 0.0085, 164.79, 1.02413e26, 24.622, 1.769 *np.pi/180],
]

for planet in planets:
    e = planet[2]
    if not (0 <= e < 1):
        raise ValueError(f"Die Exzentrizität von {planet[0]} muss zwischen 0 und 1 liegen, aber sie ist {e}.")
    
universe = tool(fig, ax)
fig, ax = universe.create_fig('black')
universe.set_axis_off()
universe.set_axis_limits(100)

sun = sphere(np.linspace(0, 2 * np.pi, 100), np.linspace(0, np.pi, 200))
sun.plot_sphere(ax, 4, 4, 'y', 0.5)

for planet in planets:
    r_planet = planet[5] * scale_sphere
    a_planet = planet[1]
    planet = sphere(r_planet * np.linspace(0, 2 * np.pi, 100), r_planet * np.linspace(0, np.pi, 100))
    planet.plot_sphere(ax, 4, 4, 'r', 0.5, x_offset=a_planet*scale_distance)

for planet in planets:
    a_planet = planet[1]
    e = planet[2]
    orbit_planet = orbit(a_planet)
    orbit_planet.plot_orbit(ax, a_planet, e, 'w', 0.5)

fig.tight_layout()
fig.subplots_adjust(left=0, right=1, top=1, bottom=0)
fig.subplots_adjust(wspace=0, hspace=0)

plt.show()



`

我已经尝试过这些功能:

fig.subplots_adjust(left=0, right=1, top=1, bottom=0) 

fig.subplots_adjust(wspace=0, hspace=0)

为什么还有左右图案。我将窗口设置为

figsize=(16, 9)

python matplotlib
1个回答
0
投票

设置每个轴的限制时会出现错误,例如:

ax.set_xlim(min,max)

在代码中将它们设置为相同的值时需要两个值

一个快速的解决方案是将您的代码调整为

self.ax.set_xlim([-size_koordsystem, size_koordsystem])

如果您的绘图以 0,0 为中心并在三个维度上对称扩展。

使用

universe.set_axis_limits(290)

它产生以下结果,可以看到所有轨道output plot

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