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.

3
  • 1
    Just pass in the ax that is returned from plotData to plot_boundary and use that to plot. Or create the ax outside of the functions and pass it in to both. Commented Sep 30, 2016 at 14:16
  • after passing what to be done.where to change Commented Sep 30, 2016 at 14:20
  • 1
    Use that ax instead of fig, ax = plt.subplots() Commented Sep 30, 2016 at 14:21

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

@Aman Not exactly - you used subplots, not subplot. Note that in subplots, you seem to be missing a 2 parameter.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.