3

I have two python plot functions:

def plotData(data):
    fig, ax = plt.subplots()
    results_accepted = data[data['accepted'] == 1]
    results_rejected = data[data['accepted'] == 0]
    ax.scatter(results_accepted['exam1'], results_accepted['exam2'], marker='+', c='b', s=40)
    ax.scatter(results_rejected['exam1'], results_rejected['exam2'], marker='o', c='r', s=30)
    ax.set_xlabel('Exam 1 score')
    ax.set_ylabel('Exam 2 score')
    return ax

And second function is:

def plot_boundry(theta,x):
    """
    """
    plt.figure(1)
    px = np.array([x[:, 1].min() - 2, x[:, 1].max() + 2])
    py = (-1 / theta[2]) * (theta[1] * px + theta[0])
    fig, ax = plt.subplots()
    ax.plot(px, py)
    return ax

And i am calling both :

#####PLOT ######
ax = plotData(df)
ax = plot_boundry(opt_theta, x)

I get 2 separate plots:
enter image description here

enter image description here

I got 2 separate picture.How Do I add two plot into one. Both the plot should be one plot.

Amaresh
  • 3,231
  • 7
  • 37
  • 60

1 Answers1

2

It depends what you want exactly:

  1. If you want the two figures overlayed, then you can call hold(True) after the first, then plot the second, then call hold(False).

  2. If you want the two figures in a single figure, but side by side (or one over the other), then you can use subplot. E.g., call subplot(2, 1, 1) before plotting the first, then subplot(2, 1, 2) before the second.

Community
  • 1
  • 1
Ami Tavory
  • 74,578
  • 11
  • 141
  • 185
  • @Aman Not exactly - you used `subplots`, not `subplot`. Note that in `subplots`, you [seem to be missing a `2` parameter](http://matplotlib.org/examples/pylab_examples/subplots_demo.html). – Ami Tavory Sep 30 '16 at 14:18