【问题标题】:python: It is possible to create 3 subplots (on both axis)?python:可以创建 3 个子图(在两个轴上)?
【发布时间】:2014-08-10 05:59:51
【问题描述】:

以下代码给出 4 (2x2) 个图:

import matplotlib.pyplot as plt
import numpy as np
x = np.arange(0,5,1)
fig, ax = plt.subplots(2,2)
ax[0,0].plot(x, x)
ax[0,1].plot(x, x*2)
ax[1,0].plot(x, np.sqrt(x))
ax[1,1].plot(x, x**2)
plt.show()

我想知道是否有任何方法可以在上面列出的前 3 个位置创建 3 个子图,同时完全摆脱右下角的 (ax[1,1])? (最终看起来一个子图与主图的 x 轴对齐,而另一个与 y 轴对齐。)

【问题讨论】:

    标签: python matplotlib plot subplot


    【解决方案1】:

    使用subplots 会生成所有子图,即使您没有在其中放置任何内容。改用subplot 怎么样?

    import matplotlib.pyplot as plt
    import numpy as np
    import matplotlib.mlab as mlab
    delta = 0.025
    x = np.arange(-3.0, 3.0, delta)
    y = np.arange(-2.0, 2.0, delta)
    X, Y = np.meshgrid(x, y)
    Z1 = mlab.bivariate_normal(X, Y, 1.0, 1.0, 0.0, 0.0)
    Z2 = mlab.bivariate_normal(X, Y, 1.5, 0.5, 1, 1)
    # difference of Gaussians
    Z = 10.0 * (Z2 - Z1)
    
    x = np.arange(0,5,1)
    plt.subplot(221).contour(X, Y, Z)
    plt.subplot(224).plot(x, x*2)
    plt.subplot(223).plot(x, np.sqrt(x))
    plt.show() 
    

    编辑:添加了改编自here 的轮廓示例。

    【讨论】:

    • 重要的不是情节的类型——而是子情节的划分。可以在axes 对象上工作的所有东西都可以在这里工作。我已经编辑了示例。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多