Python-一个绘图中的两个图形
我有两个python绘图函数:Python-一个绘图中的两个图形,python,matplotlib,Python,Matplotlib,我有两个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=
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)
我得到两个单独的绘图:我有两张独立的图片。我如何将两个情节添加到一张中。
两个绘图都应该是一个绘图。这完全取决于您想要什么:
(True)
,然后绘制第二个图形,然后调用保持(False)
子图(2,1,1)
,然后在绘制第二个图之前调用子图(2,1,2)
只需将从
plotData
返回的ax
传递到plot\u boundary
并使用它来plot
。或者在函数外部创建ax
,并将其传递给这两个函数。传递要执行的操作后。在何处更改使用该ax
,而不是fig,ax=plt.subplot()
@Aman不完全正确-您使用的是子地块,而不是子地块
。请注意,在子批次中,您可以。