Search code examples
pythonidl-programming-language

3D scatter plot


I want to create a 3D scatter plot. There is a fourth parameter which will be indicated by a colour map. I can program in Python and IDL, but was wondering has anybody created such a scatter plot and what is the best tool to use?

Thanks.


Solution

  • There are two main options matplotlib and Mayavi. At the moment matplotlib is more popular, so you will find info easier. Mayavi tends to be more advanced so you would use it if matplotlib is not enough for your use case(for example too slow). An example using matplotlib:

    import numpy as np
    from mpl_toolkits.mplot3d import Axes3D
    import matplotlib.pyplot as plt
    
    fig = plt.figure()
    ax = fig.add_subplot(111, projection='3d')
    n = 100
    
    xs = np.random.randint(0, 10, size=n)
    ys = np.random.randint(0, 10, size=n)
    zs = np.random.randint(0, 10, size=n)
    colors = np.random.randint(0, 10, size=n)
    ax.scatter(xs, ys, zs, c=colors, marker='o')
    plt.show()
    

    enter image description here

    You can find more examples here.