use*_*207 4 python plot matplotlib python-3.x
我有以下代码,首先显示matplotlib图.然后,我必须关闭第一个图,以便显示第二个图.
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import mglearn
# generate dataset
X, y = mglearn.datasets.make_forge()
# plot dataset
mglearn.discrete_scatter(X[:, 0], X[:, 1], y)
plt.legend(["Class 0", "Class 1"], loc=4)
plt.xlabel("First feature")
plt.ylabel("Second feature")
print("X.shape: {}".format(X.shape))
plt.show()
X, y = mglearn.datasets.make_wave(n_samples=40)
plt.plot(X, y, 'o')
plt.ylim(-3, 3)
plt.xlabel("Feature")
plt.ylabel("Target")
plt.show()
Run Code Online (Sandbox Code Playgroud)
我想在同一时间出现2个matplotlib图.
Imp*_*est 10
plt.show()绘制状态机中的所有数字.仅在脚本结束时调用它,可确保绘制所有先前创建的数字.
现在你需要确保每个绘图确实是在不同的图中创建的.这可以使用plt.figure(fignumber)where fignumber从索引开始的数字来实现1.
import matplotlib.pyplot as plt
import mglearn
# generate dataset
X, y = mglearn.datasets.make_forge()
plt.figure(1)
mglearn.discrete_scatter(X[:, 0], X[:, 1], y)
plt.legend(["Class 0", "Class 1"], loc=4)
plt.xlabel("First feature")
plt.ylabel("Second feature")
plt.figure(2)
X, y = mglearn.datasets.make_wave(n_samples=40)
plt.plot(X, y, 'o')
plt.ylim(-3, 3)
plt.xlabel("Feature")
plt.ylabel("Target")
plt.show()
Run Code Online (Sandbox Code Playgroud)
创建两个figures,只调用show()一次
fig1 = plt.figure()
fig2 = plt.figure()
ax1 = fig1.add_subplot(111)
ax2 = fig2.add_subplot(111)
ax1.plot(x1,y1)
ax2.plot(x2,y2)
plt.show()
Run Code Online (Sandbox Code Playgroud)