title: Plot Piecewise Function in Python
body: <p>I would like to plot the following piecewise function in Python using Matplotlib, from 0 to 5.</p>
  
  <p><code>f(x) = 1, x != 2; f(x) = 0, x = 2</code></p>
  
  <p>In Python...</p>
  
  <pre><code>def f(x):
   if(x == 2): return 0
   else: return 1
  </code></pre>
  
  <p>Using NumPy I create an array </p>
  
  <pre><code>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])
  </code></pre>
  
  <p>I have tried things like...</p>
  
  <pre><code>import matplotlib.pyplot as plt
  plt.plot(x,f(x))
  </code></pre>
  
  <p>Or...</p>
  
  <pre><code>vecfunc = np.vectorize(f)
  result = vecfunc(t)
  </code></pre>
  
  <p>Or...</p>
  
  <pre><code>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
  </code></pre>
  
  <p>But I am not using these functions correctly, and am now just randomly guessing how to do this. </p>
  
  <p>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?</p>
  
  <p><a href="https://i.stack.imgur.com/aR7VL.png" rel="noreferrer"><img src="https://i.stack.imgur.com/aR7VL.png" alt="enter image description here"></a></p>