Dan Ciborowski - MSFT
Dan Ciborowski - MSFT

Reputation: 7207

Plot Piecewise Function in Python

I would like to plot the following piecewise function in Python using Matplotlib, from 0 to 5.

f(x) = 1, x != 2; f(x) = 0, x = 2

In Python...

def f(x):
 if(x == 2): return 0
 else: return 1

Using NumPy I create an array

x = np.arange(0., 5., 0.2)

    array([ 0. ,  0.2,  0.4,  0.6,  0.8,  1. ,  1.2,  1.4,  1.6,  1.8,  2. ,
        2.2,  2.4,  2.6,  2.8,  3. ,  3.2,  3.4,  3.6,  3.8,  4. ,  4.2,
        4.4,  4.6,  4.8])

I have tried things like...

import matplotlib.pyplot as plt
plt.plot(x,f(x))

Or...

vecfunc = np.vectorize(f)
result = vecfunc(t)

Or...

def piecewise(x):
 if x == 2: return 0
 else: return 1

import matplotlib.pyplot as plt
x = np.arange(0., 5., 0.2)
plt.plot(x, map(piecewise, x))

ValueError: x and y must have same first dimension

But I am not using these functions correctly, and am now just randomly guessing how to do this.

Some answers are starting to get there... But the points are being connected into a line on the plot. How do we just plot the points?

enter image description here

Upvotes: 14

Views: 55157

Answers (6)

gboffi
gboffi

Reputation: 25023

Your function is continuous except for an interval of zero measure. In my opinion the correct way to plot it is

In [8]: import matplotlib.pyplot as plt
   ...: plt.plot((0, 5), (1, 1), color='blue', label='Discontinuos function')
   ...: plt.scatter(2, 0, color='blue')
   ...: plt.grid()
   ...: plt.legend()
   ...: plt.show()

In [9]: 

enter image description here

Upvotes: 1

Yichang Wu
Yichang Wu

Reputation: 61

if you are using python 2.x, map() returns a list. so you can write code as this:

import matplotlib.pyplot as plt
import numpy as np


def f(t):
    if t < 10:
        return 0;
    else:
        return t * t - 100;


t = np.arange(0, 50, 1)

plt.plot(t, map(f, t), 'b-')

plt.show()

if you are using python 3.x, map() returns a iterator. so convert the map to a list.

plt.plot(t, list(map(f, t)), 'b-')

Upvotes: 0

guesto
guesto

Reputation: 1

the append works but requires a little extra processing. np's piecewise works fine. could just do this for any function:

`

import math
import matplotlib as plt

xs=[]
xs=[x/10 for x in range(-50,50)]   #counts in tenths from -5 to 5

plt.plot(xs,[f(x) for x in xs])

`

Upvotes: 0

litepresence
litepresence

Reputation: 3277

Some answers are starting to get there... But the points are being connected into a line on the plot. How do we just plot the points?

import matplotlib.pyplot as plt
import numpy as np

def f(x):
 if(x == 2): return 0
 else: return 1

x = np.arange(0., 5., 0.2)

y = []
for i in range(len(x)):
   y.append(f(x[i]))

print x
print y

plt.plot(x,y,c='red', ls='', ms=5, marker='.')
ax = plt.gca()
ax.set_ylim([-1, 2])

plt.show()

enter image description here

Upvotes: 13

Padraic Cunningham
Padraic Cunningham

Reputation: 180411

You can use np.piecewise on the array:

x = np.arange(0., 5., 0.2)
import matplotlib.pyplot as plt
plt.plot(x, np.piecewise(x, [x  == 2, x != 2], [0, 1]))

Upvotes: 4

enrico.bacis
enrico.bacis

Reputation: 31504

The problem is that the function f does not take an array as input but a single numer. You can:

plt.plot(x, map(f, x))

The map function takes a function f, an array x and returns another array where the function f is applied to each element of the array.

Upvotes: 8

Related Questions