2D stem plot

asked 2013-05-29 07:57:50 -0500

this post is marked as community wiki

This post is a wiki. Anyone with karma >750 is welcome to improve it.

Hi, this is not really a question but I come by these days to search for a stem plot, can't find any useful and finally came up with the following solution, I want to share:

def stem(data,offset=0,**kwargs):
p=point([])
l=line([])
for d in range(len(data)):
    p+=point((d+offset,data[d]),size=50)
    l+=line([(d+offset,0),(d+offset,data[d])])
show(p+l,xmax=len(data),**kwargs)

for example:

values=[]
for v in range(0,5):
    values.append(1/4^v)
stem(values,ymax=1,axes_labels=('n','x[n]'))
#or with offset
stem(values,-4,ymax=1,axes_labels=('n','x[n]'))
  • arg1 list of data
  • arg2 gives you the opportunity to adjust the starting point
  • **kwargs passes all following arguments to the show-function
edit retag flag offensive close merge delete

Comments

better use: >>> show(p+l,xmax=len(data)-1+offset,**kwargs)

farnzworld gravatar imagefarnzworld ( 2013-05-29 08:13:10 -0500 )edit

Is this a typical thing used in certain types of data analysis? If so, we could open a ticket for adding something like this - perhaps we could wrap [matplotlib's](http://matplotlib.org/1.2.0/api/pyplot_api.html?highlight=stem#matplotlib.pyplot.stem).

kcrisman gravatar imagekcrisman ( 2013-05-29 17:08:42 -0500 )edit

Yes. Used in statistics.

ppurka gravatar imageppurka ( 2013-05-29 19:36:35 -0500 )edit

Great, I've made this [Trac 14663](http://trac.sagemath.org/sage_trac/ticket/14663).

kcrisman gravatar imagekcrisman ( 2013-05-30 02:03:56 -0500 )edit