【问题标题】:Extending gridlines in a 3d matplotlib plot在 3d matplotlib 图中扩展网格线
【发布时间】:2021-10-28 13:29:18
【问题描述】:

有没有办法将网格线扩展到 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()

谢谢!

【问题讨论】:

    标签: python matplotlib 3d gridlines


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-19
      • 1970-01-01
      • 2011-12-17
      • 1970-01-01
      • 2012-07-30
      • 1970-01-01
      相关资源
      最近更新 更多