Python 中一个子图中的 Scatter 和 Hist

Scatter and Hist in one subplot in Python

这是代码

    df = pd.DataFrame(3 * np.random.rand(4, 2), columns=['a', 'b'])
    plt.subplot(121)
    df["a"].plot.box()
    plt.subplot(122)
    df.plot.scatter(x="a", y="b")
    plt.show()

输出有两种不同的 windows,如下所示:-

图1

图2

虽然两者应该合二为一window。任何错误的建议

调用scatter时需要指定在哪个轴上绘制。这可以通过将 ax = 参数传递给绘图函数来完成:

df = pd.DataFrame(3 * np.random.rand(4, 2), columns=['a', 'b'])
plt.subplot(121)
df["a"].plot.box()
ax = plt.subplot(122)
df.plot.scatter(x="a", y="b", ax = ax)
plt.show()