[Python] How to remove padding between the plot and window

Discussão em 'Python' iniciado por Stack, Dezembro 21, 2024.

  1. Stack

    Stack Membro Participativo

    I'm trying to rebuild the solar system in a 3D - koordinate system. I build one sphere as the sun and multiple circles (orbits) around it with sphere as planets. But I don't know how I can remove the padding between the plot and window so you can see the right and left side of the orbit.

    Picture - Solar System but the outer orbits are cut out on the left and right

    `

    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()



    `


    I already tried these functions:

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

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

    Why is there still a left and right pattern. I set the window to figsize=(16, 9)

    Continue reading...

Compartilhe esta Página