首页 > 解决方案 > Matplotlib - 如何将数字彼此相邻放置?

问题描述

我有以下代码:

def compare(f,a,b,c,d,n,points):
    """Plots 2 figures - one of the color map of f, and one of the color map of a rectangle [a,b] x [c,d], split
    into n^2 subareas, using the list of points to estimate the color map"""
    #fig, axes = plt.subplots(nrows=2, ncols=2)
    q = plt.figure(1)
    colorMapList(f,a,b,c,d,n,points)
    #q.show()
    p = plt.figure(2)
    colorMap(f)
    plt.show()

函数colorMapListcolorMap都返回ax.contourf(Y,X,z)

当我按照我的方式编写代码时,程序会输出两个图表,一个在另一个之下。我怎样才能让图表彼此水平显示?谢谢!

标签: pythonmatplotlibfiguresubplot

解决方案


如果您希望两个图都在一个图形上,那么您可以使用plt.subplot(121)plt.subplot(122)。第一个索引是行数,第二个索引是列数。第三个索引是图形布局的位置计数,因此如果它是 subplot(221) 将是 2x2 显示的图形,1 表示左上角的图形。然后,subplot(222) 将位于右上角,subplot(223) 位于左下角,subplot(224) 位于右下角。这遵循每行从左上到右的顺序。

但是,如果您想并排绘制 2 个不同的图形,则可以查看解决方案。


推荐阅读