Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I plot a step function with Matplotlib in Python?

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 
like image 265
rhm2012 Avatar asked Jan 19 '12 05:01

rhm2012


People also ask

Can matplotlib do 3D graphs?

In order to plot 3D figures use matplotlib, we need to import the mplot3d toolkit, which adds the simple 3D plotting capabilities to matplotlib. Once we imported the mplot3d toolkit, we could create 3D axes and add data to the axes. Let's first create a 3D axes.

Can matplotlib plot a function?

Quadratic EquationPlotting a quadratic function is almost the same as plotting the straight line in the previous tutorial. Below is the Matplotlib code to plot the function y=x2 y = x 2 . It is a simple straight-forward code; the bulk of it in the middle is for setting the axes.


2 Answers

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

like image 154
Joe Kington Avatar answered Sep 28 '22 04:09

Joe Kington


If you have non-uniformly spaced data points, you can use the drawstyle keyword argument for plot:

x = [1,2.5,3.5,4]  y = [0.002871972681775004, 0.00514787917410944,       0.00863476098280219, 0.012003316194034325]  plt.plot(x, y, drawstyle='steps-pre') 

Also available are steps-mid and steps-post.

like image 45
Will Vousden Avatar answered Sep 28 '22 04:09

Will Vousden