子图无法正常工作

Subplot not working properly

这是我为生成 2 x 2 子图而编写的代码。

代码如下:

import math
import numpy
import functools
import operator
from fractions import Fraction
import matplotlib.pyplot as plt
import pylab
from pylab import *


def nCk(n, k):
    return int(functools.reduce(operator.mul,
                                (Fraction(n-i, i+1) for i in range(k)), 1))

Theta = 0.5 cList = numpy.arange(-5, 5, 1) NList = [5, 10, 100, 1000]

for i in list(range(len(NList))):
    N = NList[i]
    print(N)
    dList = []

    for c in cList:
        print(c)
        var = math.floor(float(N*Theta) + float(c*Theta*(1-Theta)*math.sqrt(N)))
        dList.append(pow(0.5, N)*(sum(nCk(N, x) for x in range(var))))

    print([1+math.floor(i/2), 1+i % 2])

    # plt.subplot(4,1+math.floor(i/2),1+i%2)
    # plt.plot(cList,dList)
    # plt.title([1+math.floor(i/2),1+i%2])

    subplot(4, 1+math.floor(i/2), 1+i % 2)
    fig = plot(cList, dList)


# plt.savefig('C:/Users/Sumit_G/foo1.png')
pylab.savefig('C:/Users/Sumit_G/foo.png')

但是,不是得到 2 x 2 图。

我正在生成下面的图:

尝试:

    fig = plt.figure()
    for i in range(4):
        ax = fig.add_subplot(2,2,i+1)
        ax.plot(whatever)

您不必要地贬低和 mod-ing 您的价值观。 Subplot 接受 3 个参数 subplot(nrows, ncols, plot_number) (http://matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.subplot),其中 plot_number 是从 1(左上角)开始到 nrows*ncols(右下角)结束的活动图。见附图。