在 3d matplotlib 图中扩展网格线

Extending gridlines in a 3d matplotlib plot

有什么方法可以将网格线扩展到 3d 图的数据区域吗?

我有这个图,所有图的 z 轴都为 1(通过检查数组值确认),但这对我来说并不明显。我希望添加内部网格线将有助于阐明它。我用这段代码生成了它:

fig = plt.figure(figsize = (10,10))
ax = plt.axes(projection ='3d')
x, y, z = np.array(list1), np.array(list2), np.array(list3) 
c = x+y
ax.scatter(x, y, z, c=c)
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_zlabel('z')
ax.set_title(f'Title')
plt.show()

谢谢!

选项 1

  • 如果所有点都在一个 Z 值,则只绘制一个平面
import matplotlib.pyplot as plt
import numpy as np

# test data
np.random.seed(365)
x, y = np.random.randint(2500, 20000, size=(7, )), np.random.randint(0, 1600, size=(7, ))

fig, ax = plt.subplots(figsize=(6, 6))
ax.scatter(x=x, y=y)
ax.set(title='Plane at Z=1')

选项 2

  • 在轴刻度处绘制线条
import matplotlib.pyplot as plt
import numpy as np

fig = plt.figure(figsize=(10, 10))
ax = plt.axes(projection='3d')

# test data
np.random.seed(365)
x, y, z = np.random.randint(2500, 20000, size=(7, )), np.random.randint(0, 1600, size=(7, )), np.ones(7) 

c = x+y
ax.scatter(x, y, z, c=c)

# get the x and y tick locations
x_ticks = ax.get_xticks()
y_ticks = ax.get_yticks()

# add lines
for y1 in y_ticks:
    x1, x2 = x_ticks[0], x_ticks[-1]
    ax.plot([x1, x2], [y1, y1], [1, 1], color='lightgray')
for x1 in x_ticks:
    y1, y2 = y_ticks[0], y_ticks[-1]
    ax.plot([x1, x1], [y1, y2], [1, 1], color='lightgray')
    
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_zlabel('z')
ax.set_title(f'Title')
plt.show()