Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a matplotlib counterpart of Matlab "stem3"?

It is quite easy to make a 3d stem plot with stem3 command as documented in http://www.mathworks.com/help/techdoc/ref/stem3.html I wonder if there is a similar command in matplotlib? I checked the online document for the latest version, but could not find one. Can anyone give some suggestions?

like image 493
ljxue Avatar asked Dec 10 '11 01:12

ljxue


2 Answers

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

like image 116
unutbu Avatar answered Sep 30 '22 06:09

unutbu


As unutbu's, just with a conventional plot:

In [20]: from mpl_toolkits.mplot3d import Axes3D
In [21]: fig = figure()
In [22]: ax = fig.add_subplot(111, projection='3d')
In [23]: x = [2,4,1,3]
In [24]: y = [3,5,6,7]
In [25]: z = [4,5,6,7]  
In [26]: for xx,yy,zz in zip(x,y,z): plot([xx,xx],[yy,yy],[0,zz], '-')

enter image description here

like image 29
joaquin Avatar answered Sep 30 '22 05:09

joaquin