我有以下代码,首先显示 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()
我想让 2 个 matplotlib 图同时出现。
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()
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)