python - 在 3d matplotlib 图中扩展网格线

标签 python matplotlib 3d gridlines

有什么方法可以将网格线扩展到 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()

enter image description here

谢谢!

最佳答案

选项 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')

enter image description here

选项 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()

enter image description here

enter image description here

关于python - 在 3d matplotlib 图中扩展网格线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68973206/

相关文章:

python - 句子打印太多次

python - py2exe错误: "Error in atexit._run_exitfuncs:Error in sys.exitfunc:"

python - 绑定(bind)或命令以获得返回和按钮工作

matplotlib - 为 pyplot 对象设置 alpha channel ?

java - 以编程方式更改模型纹理 libgdx

javascript - Three.js 纹理未加载 - 是什么原因导致的?

python - Thrift Python 客户端中具有默认值的默认参数

python - 在 Jupyter Notebook 上清理并制作可读的条形图

python - matplotlib pyplot 不显示轴刻度或标签

javascript - 如何在 Three.js 中将场景或模型导出为 GLTF 格式?