0

I'm working with 2-D matrices, where the entries are elevation, and am trying to plot them with MatPlotLib mplot3d. How can I adjust the vertical height of the resulting figure? To give an example, pulling from code in the mplot3d tutorial:

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


fig = plt.figure()
ax = fig.gca(projection='3d')

# Make data.
X = np.arange(-5, 5, 0.25)
Y = np.arange(-5, 5, 0.25)
X, Y = np.meshgrid(X, Y)
R = np.sqrt(X**2 + Y**2)
Z = np.sin(R)

# Plot the surface.
surf = ax.plot_surface(X, Y, Z, linewidth=0, antialiased=False)

# Configure z-axis.
ax.set_zlim(-1.01, 1.01)

plt.show()

This gives:

Figure 1

I'd like to compress the picture vertically so that the 1.00 mark on the z-axis gets pushed down to present position of -0.75.

Changing the z-limit with ax.set_zlim(-1.01, 10.01) flattens the data but keeps the figure the same height. I've also experimented with statements like plt.figure(figsize=(20,5)), but that changes the figure size and interferes with rotating the resulting plot in 3-D.

Figure 2

How do I compress down just the z-axis?

  • 1
    Try [this](https://stackoverflow.com/questions/13685386/matplotlib-equal-unit-length-with-equal-aspect-ratio-z-axis-is-not-equal-to) and [this](https://stackoverflow.com/questions/30223161/matplotlib-mplot3d-how-to-increase-the-size-of-an-axis-stretch-in-a-3d-plo) – Sheldore Feb 19 '20 at 00:22

1 Answers1

0

Thanks to @sheldore—this answer does the trick.

Adding:

scale_x = 1
scale_y = 1
scale_z = 0.25

ax.get_proj = lambda: np.dot(Axes3D.get_proj(ax), np.diag([scale_x, scale_y, scale_z, 1]))

Gives the following plot, which will do exactly what I want after tinkering with axis numbering.

Figure 3