This should be easy but I have just started toying with matplotlib and python. I can do a line or a scatter plot but i am not sure how to do a simple step function. Any help is much appreciated.

x = 1,2,3,4
y = 0.002871972681775004, 0.00514787917410944, 0.00863476098280219, 0.012003316194034325
link|improve this question

80% accept rate
What do you mean by a step function? Like a histogram? – wim Jan 19 at 5:14
feedback

3 Answers

up vote 2 down vote accepted

It seems like you want step.

E.g.

import matplotlib.pyplot as plt

x = [1,2,3,4] 
y = [0.002871972681775004, 0.00514787917410944, 
     0.00863476098280219, 0.012003316194034325]

plt.step(x, y)
plt.show()

enter image description here

link|improve this answer
Thanks! It works. Any way to get rid of the vertical lines? – rhm2012 Jan 19 at 5:52
2  
Well, if you don't want any vertical lines, have a look at plt.hlines. E.g. plt.hlines(y, range(1,5), range(2,6)) – Joe Kington Jan 19 at 6:17
feedback

Just draw two lines, one at y=0, and one at y=1, cutting off at whatever x your step function is for?

e.g. if you want to step from 0 to 1 at x=2.3 and plot from x=0 to x=5:

import matplotlib.pyplot as plt
#                                 _
# if you want the vertical line _|
plt.plot([0,2.3,2.3,5],[0,0,1,1])
#
# OR:
#                                       _
# if you don't want the vertical line _
#plt.plot([0,2.3],[0,0],[2.3,5],[1,1])

# now change the y axis so we can actually see the line
plt.ylim(-0.1,1.1)

plt.show()
link|improve this answer
feedback

I think you want pylab.bar(x,y,width=1) or equally pyplot's bar method. if not checkout the gallery for the many styles of plots you can do. Each image comes with example code showing you how to make it using matplotlib.

link|improve this answer
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.