pythonmatplotlibmatplotlib-3dscatter3d

Set zlim in matplotlib scatter3d


I have three lists xs, ys, zs of data points in Python and I am trying to create a 3d plot with matplotlib using the scatter3d method.

import matplotlib.pyplot as plt

fig = plt.figure()  
ax = fig.add_subplot(111, projection='3d')  
plt.xlim(290)  
plt.ylim(301)  
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
ax.scatter(xs, ys, zs)  
plt.savefig('dateiname.png')
plt.close()

The plt.xlim() and plt.ylim() work fine, but I don't find a function to set the borders in z-direction. How can I do so?


Solution

  • Simply use the set_zlim function of the axes object (like you already did with set_zlabel, which also isn't available as plt.zlabel):

    import matplotlib.pyplot as plt
    import numpy as np
    
    xs = np.random.random(10)
    ys = np.random.random(10)
    zs = np.random.random(10)
    
    fig = plt.figure()  
    ax = fig.add_subplot(111, projection='3d')  
    ax.set_xlabel('X')
    ax.set_ylabel('Y')
    ax.set_zlabel('Z')
    ax.scatter(xs, ys, zs)  
    ax.set_zlim(-10, 10)
    

    enter image description here