pylab 3d scatter plots with 2d projections of plotted data

后端 未结 2 491
时光取名叫无心
时光取名叫无心 2020-12-05 20:52

I am trying to create a simple 3D scatter plot but I want to also show a 2D projection of this data on the same figure. This would allow to show a correlation between two o

相关标签:
2条回答
  • 2020-12-05 21:11

    The other answer works with matplotlib 0.99, but 1.0 and later versions need something a bit different (this code checked with v1.3.1):

    import numpy as np
    import matplotlib.pyplot as plt
    from mpl_toolkits.mplot3d import Axes3D
    
    x= np.random.random(100)
    y= np.random.random(100)
    z= np.sin(3*x**2+y**2)
    
    fig= plt.figure()
    ax = Axes3D(fig)
    ax.scatter(x,y,z)
    
    ax.plot(x, z, 'r+', zdir='y', zs=1.5)
    ax.plot(y, z, 'g+', zdir='x', zs=-0.5)
    ax.plot(x, y, 'k+', zdir='z', zs=-1.5)
    
    ax.set_xlim([-0.5, 1.5])
    ax.set_ylim([-0.5, 1.5])
    ax.set_zlim([-1.5, 1.5])
    
    plt.show() 
    

    You can see what version of matplotlib you have by importing it and printing the version string:

    import matplotlib
    print matplotlib.__version__
    
    0 讨论(0)
  • 2020-12-05 21:18

    You can add 2D projections of your 3D scatter data by using the plot method and specifying zdir:

    import numpy as np
    import matplotlib.pyplot as plt
    
    x= np.random.random(100)
    y= np.random.random(100)
    z= np.sin(3*x**2+y**2)
    
    fig= plt.figure()
    ax= fig.add_subplot(111, projection= '3d')
    ax.scatter(x,y,z)
    
    ax.plot(x, z, 'r+', zdir='y', zs=1.5)
    ax.plot(y, z, 'g+', zdir='x', zs=-0.5)
    ax.plot(x, y, 'k+', zdir='z', zs=-1.5)
    
    ax.set_xlim([-0.5, 1.5])
    ax.set_ylim([-0.5, 1.5])
    ax.set_zlim([-1.5, 1.5])
    
    plt.show()
    

    enter image description here

    0 讨论(0)
提交回复
热议问题