29

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?

Bart
  • 9,825
  • 5
  • 47
  • 73
Jann
  • 635
  • 1
  • 6
  • 17

1 Answers1

45

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)
Bart
  • 9,825
  • 5
  • 47
  • 73
  • 14
    Why is `plt.zlim` not implemented? That would make mpl more consistent and people would not have to ask this question here. – Soerendip Sep 26 '19 at 18:40
  • @Sören. 3D plotting is an extension. That being said, I don't see any reason why you can't have the relevant import set up an addition function in the plt namespace – Mad Physicist Jun 20 '20 at 04:36