Set zlim in matplotlib scatter3d

Jann picture Jann · May 30, 2016 · Viewed 25.5k times · Source

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?

Answer

Bart picture Bart · May 30, 2016

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
from mpl_toolkits.mplot3d import Axes3D
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)