2016-09-30 40 views
3

我有兩個Python情節功能:Python的 - 兩個人物在一個情節

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 

而第二個功能是:

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 

,我打電話都:

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

我得到2個獨立的地塊:
enter image description here

enter image description here

我得到了2張獨立的照片。我如何將兩張圖添加到一張。 這兩個情節應該是一個情節。

+1

只需傳遞從'plotData'返回到'plot_boundary'的'ax'並將其用於'plot'。或者在函數之外創建'ax'並將它傳遞給兩者。 – AChampion

+0

在傳遞完成之後,要改變的地方 – Aman

+1

使用'ax'代替'fig,ax = plt.subplots()' – AChampion

回答

2

這取決於你想要什麼確切:

  1. 如果你想這兩個數字疊加,那麼你可以調用後的第一個hold(True),然後繪製第二個,然後調用hold(False)

  2. 如果你想在一個圖中顯示兩個數字,但並排(或者一個在另一個),那麼你可以使用subplot。例如,在繪製第一個之前,然後在subplot(2, 1, 2)之前調用subplot(2, 1, 1)

+0

我已經使用subplot() – Aman

+0

@Aman不完全 - 你使用了'subplots',而不是'subplot'。請注意,在'subplots'中,你[似乎缺少'2'參數](http://matplotlib.org/examples/pylab_examples/subplots_demo.html)。 –