title: How to plot the decision boundary line which same with the decision boundary of plt.contourf?
body: <p>From <a href="https://www.llipe.com/2017/04/19/programando-un-clasificador-perceptron-en-python/" rel="nofollow noreferrer">this</a> link:</p>
  
  <p><a href="https://i.stack.imgur.com/3uaj1.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/3uaj1.png" alt="enter image description here"></a>
  As shown in function <code>plot_decision_regions</code>, the decision regions can be visualized by dense sampling via meshgrid. However, if the grid resolution is not enough, as artificially set below, the boundary will appear inaccurate.</p>
  
  <p>Implement function <code>plot_decision_boundary</code> below to analytically compute and plot the decision boundary.</p>
  
  <p>Therefore, I would like to ask how can I change the function of <code>plot_decision_boundary</code> to display the line which is same with the decision boundary of <code>plt.contourf()</code>. (like the green line)</p>
  
  <p><a href="https://i.stack.imgur.com/uVGj6.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/uVGj6.png" alt="enter image description here"></a></p>
  
  <pre class="lang-py prettyprint-override"><code>from matplotlib.colors import ListedColormap
  
  def plot_decision_regions(X, y, classifier, resolution=0.01):
      markers = ('s', 'x', 'o', '^', 'v')
      colors = ('red', 'blue', 'lightgreen', 'gray', 'cyan')
      cmap = ListedColormap(colors[:len(np.unique(y))])
      x1_min, x1_max = X[:, 0].min() - 1, X[:, 0].max() + 1
      x2_min, x2_max = X[:, 1].min() - 1, X[:, 1].max() + 1
      xx1, xx2 = np.meshgrid(np.arange(x1_min, x1_max, resolution),
                             np.arange(x2_min, x2_max, resolution))
      Z = classifier.predict(np.array([xx1.ravel(), xx2.ravel()]).T)
      Z = Z.reshape(xx1.shape)
      plt.contourf(xx1, xx2, Z, alpha=0.3, cmap=cmap)
      plt.xlim(xx1.min(), xx1.max())
      plt.ylim(xx2.min(), xx2.max())
      for idx, cl in enumerate(np.unique(y)):
          plt.scatter(x=X[y == cl, 0], 
                      y=X[y == cl, 1],
                      alpha=0.8, 
                      c=colors[idx],
                      marker=markers[idx], 
                      label=cl, 
                      edgecolor='black')
  
  def plot_decision_boundary(X, y, classifier):       
      # replace the two lines below with your code
      x1_interval = [X[:, 0].min() - 1, X[:, 0].max() + 1]
      x2_interval = [X[:, 1].min() - 1, X[:, 1].max() + 1]
  
      plt.plot(x1_interval, x2_interval, color='green', linewidth=4, label='boundary')
  
  low_res = 0.1 # intentional for this exercise
  plot_decision_regions(X, y, classifier=ppn, resolution=low_res)
  plot_decision_boundary(X, y, classifier=ppn)
  plt.xlabel('sepal length [cm]')
  plt.ylabel('petal length [cm]')
  plt.legend(loc='upper left')
  plt.tight_layout()
  plt.show()
  </code></pre>