Search code examples
pythonmatlabmatplotlibstem-plot

Is there a matplotlib counterpart of MATLAB "stem3"?


In MATLAB, it is quite easy to make a 3d stem plot with the stem3 command.

Is there a similar command in matplotlib? I checked the online documentation for the latest version, but could not find one. Can anyone give some suggestions on how to plot this data as a 3d stem plot?

import numpy as np

N = 50
theta = np.linspace(0, 2*np.pi, N, endpoint=False)
x = np.cos(theta)
y = np.sin(theta)
z = range(N)

Solution

  • I'm unaware of any direct equivalent of stem3 in matplotlib. However, it isn't hard to draw such figures (at least in its basic form) using Line3Ds:

    import matplotlib.pyplot as plt
    import mpl_toolkits.mplot3d.art3d as art3d
    import numpy as np
    
    fig = plt.figure()
    ax = fig.add_subplot(1, 1, 1, projection='3d')
    
    N = 100
    theta = np.linspace(0, 2*np.pi, N, endpoint=False)
    x = np.cos(theta)
    y = np.sin(theta)
    z = range(N)
    for xi, yi, zi in zip(x, y, z):        
        line=art3d.Line3D(*zip((xi, yi, 0), (xi, yi, zi)), marker='o', markevery=(1, 1))
        ax.add_line(line)
    ax.set_xlim3d(-1, 1)
    ax.set_ylim3d(-1, 1)
    ax.set_zlim3d(0, N)    
    plt.show()
    

    enter image description here