我正在尝试使用pyplot
中的matplotlib
创建两个图形。我希望并排看到它们,但是第二个图形仅在我关闭第一个图形之后才打开。另外,如果有帮助,我也在Windows 10的anaconda上使用vscode
。
我尝试只在结尾使用plt.show()
一次,但这只显示第二张图,而不显示第一张图。
这是我的代码:
# Visualising the Training set results
plt.scatter(X_train, y_train, color = 'red')
plt.plot(X_train, regressor.predict(X_train), color = 'blue')
plt.title('Salary vs Experience (Training Set Results)')
plt.xlabel('Years of Experience')
plt.ylabel('Salary')
# Visualising the Test set results
plt.scatter(X_test, y_test, color = 'red')
plt.plot(X_train, regressor.predict(X_train), color = 'blue')
plt.title('Salary vs Experience (Test Set Results)')
plt.xlabel('Years of Experience')
plt.ylabel('Salary')
plt.show()
如果必要的话,我可以在上面添加代码,但我认为那只会很混乱。
编辑:我发现下面的代码完成了我想要的
# Create figure to visualize results
fig = plt.figure()
ax1 = fig.add_subplot(211)
ax2 = fig.add_subplot(212)
# Visualising the Training set results
ax1.scatter(X_train, y_train, color = 'red')
ax1.plot(X_train, regressor.predict(X_train), color = 'blue')
ax1.title.set_text('Salary vs Experience (Training Set Results)')
ax1.axes.set_xlabel('Years of Experience')
ax1.axes.set_ylabel('Salary')
# Visualising the Test set results
ax2.scatter(X_test, y_test, color = 'red')
ax2.plot(X_train, regressor.predict(X_train), color = 'blue')
ax2.title.set_text('Salary vs Experience (Test Set Results)')
ax2.axes.set_xlabel('Years of Experience')
ax2.axes.set_ylabel('Salary')
plt.show()