How to remove auto scaling in matplotlib 3D plot

108 Views Asked by At

There is small doubt, when I try to extend the following graph to some bigger value of x lets say x=1000 keeping all other parameter same. The graph gets shrunk in x direction but I want it to get extended along x without auto scaling so that i can bind a horizontal scrollbar to it. As I need to add some more plots on the surface along x direction. So it will not be visible if it gets shrunk. I request you to suggest me how to achieve that. Following is my code:

import matplotlib.pyplot as plt
import numpy as np
from mpl_toolkits.mplot3d.art3d import Poly3DCollection

# Create a 3D figure
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.view_init(elev=0, azim=180,vertical_axis='y')

# Define the vertices of the rectangular rod
N_sub = 1000  # Set higher for better resolution; lower for better performance!
vertices = np.concatenate([
    np.array([[x, -0.5, -0.5],
              [x, -0.5, 0.5],
              [x, 0.5, 0.5],
              [x, 0.5, -0.5]])
    for x in np.linspace(-1000, 0, N_sub + 1)], axis=0)

# Define the faces of the rectangular rod
offset = (N_sub) * 4
faces = np.concatenate([
    [[0, 1, 2, 3]],
    *[np.array([[3, 0, 4, 7],
                [0, 1, 5, 4],
                [1, 2, 6, 5],
                [2, 3, 7, 6]]) + 4 * x for x in range(0, N_sub)],
    [[0 + offset, 1 + offset, 2 + offset, 3 + offset]]
], axis=0)

# Map the x-coordinate of each face to a color in the colormap
x = np.array([np.mean([vertices[idx, 0] for idx in face]) for face in faces])
norm = plt.Normalize(x.min(), x.max())
cmap = plt.get_cmap("hot")
colors = cmap(norm(x))

# Create a Poly3DCollection object with the vertices, faces, and colors
rect = Poly3DCollection(vertices[faces], alpha=0.9, facecolors=colors)

# Add the rectangular rod to the plot
ax.add_collection3d(rect)

# Set the limits of the x, y, and z axes
ax.set_xlim([-1000, 0])
ax.set_ylim([-1, 1])
ax.set_zlim([-1, 1])


# Add labels to the axes
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')

# Show the plot
plt.show()

I tred set_box_aspect method but it doesn't work for me. I have also tried adjusting fig size but nothing works. I dont want a cuboidal box type plot I need to stretch it to its actual length.

0

There are 0 best solutions below