如何创建循环以遍历 python 中的输入变量?

How to make a loop for going through the input variables in python?

我想通过创建某种循环来修改此代码,该循环可以遍历变量名,所以我不应该写下 'if data1 is not None' 部分?我也想知道是否有一种方法可以制作某种动态代码,使函数的输入数量可以以某种方式改变,例如,假设我想输入 100 个不同的数据集,我无法写下来所有这些都在功能的输入部分,我该怎么办? 另外,我怎么能为这两个地块加上标题呢?因为当我使用 plt.title() 时,它只显示最后一个标题。

import numpy as np
import matplotlib.pyplot as plt


np.random.seed(4)

randomSet = np.random.randint(0, 2, (10, 20))

np.random.seed(3)
randomSet3 = np.random.randint(0, 2, (10, 20))

np.random.seed(2)
randomSet2 = np.random.randint(0, 2, (10, 20))

np.random.seed(1)
randomSet1 = np.random.randint(0, 2, (10, 20))


    def showResult(data, data1 = None, data2 = None, data3 = None, data4 = None, data5 = None, nscan = 1):
    #index = 0
    total = np.zeros(data.shape[0]*data.shape[1])
    dataList = [data.reshape(data.shape[0]*data.shape[1])]

    if data1 is not None:
        dataList.append(data1.reshape(data1.shape[0]*data1.shape[1]))


    if data2 is not None:
        dataList.append(data2.reshape(data2.shape[0]*data2.shape[1]))


    if data3 is not None:
        dataList.append(data3.reshape(data3.shape[0]*data3.shape[1]))


    if data4 is not None:
        dataList.append(data4.reshape(data4.shape[0]*data4.shape[1]))

    if data5 is not None:
        dataList.append(data5.reshape(data5.shape[0]*data5.shape[1]))

    #total = copy.copy(data) 


    for i in range(nscan):
        total += dataList[i]               




    fig = plt.figure(figsize = (8, 10))
    ax1 = fig.add_subplot(211)
    ax2 = fig.add_subplot(212)   
    ax1.imshow(total.reshape(data.shape[0], data.shape[1]), cmap= 'gray', interpolation= 'nearest')
    #plt.title('Image')

    ax2.hist(total)
    #plt.title('Histogram')
    plt.show()
    return total

showResult(randomSet, randomSet1, randomSet, randomSet3, randomSet, randomSet2, nscan= 6)

输出应该是:

array([ 1.,  2.,  5.,  4.,  4.,  2.,  4.,  3.,  2.,  5.,  0.,  3.,  5.,
        6.,  2.,  5.,  5.,  5.,  0.,  0.,  0.,  2.,  2.,  1.,  2.,  0.,
        4.,  0.,  5.,  4.,  4.,  4.,  1.,  6.,  2.,  1.,  3.,  1.,  4.,
        1.,  2.,  4.,  1.,  3.,  5.,  3.,  1.,  5.,  2.,  4.,  4.,  1.,
        1.,  3.,  1.,  6.,  3.,  5.,  5.,  1.,  3.,  5.,  4.,  1.,  4.,
        3.,  5.,  5.,  4.,  5.,  2.,  1.,  4.,  1.,  2.,  1.,  6.,  3.,
        2.,  4.,  5.,  1.,  1.,  2.,  5.,  3.,  2.,  5.,  3.,  2.,  3.,
        3.,  4.,  1.,  4.,  2.,  5.,  2.,  4.,  5.,  5.,  5.,  1.,  4.,
        5.,  0.,  4.,  1.,  5.,  1.,  5.,  2.,  2.,  2.,  1.,  3.,  1.,
        1.,  3.,  1.,  3.,  3.,  5.,  5.,  5.,  2.,  2.,  1.,  4.,  5.,
        2.,  5.,  2.,  3.,  2.,  0.,  0.,  5.,  5.,  5.,  2.,  2.,  1.,
        1.,  4.,  4.,  4.,  2.,  5.,  2.,  4.,  5.,  4.,  2.,  2.,  1.,
        4.,  4.,  2.,  4.,  4.,  1.,  4.,  3.,  5.,  0.,  1.,  2.,  3.,
        0.,  5.,  3.,  2.,  2.,  2.,  4.,  4.,  2.,  4.,  0.,  5.,  5.,
        2.,  3.,  0.,  1.,  1.,  5.,  3.,  1.,  3.,  5.,  1.,  2.,  3.,
        5.,  5.,  2.,  2.,  5.])

Output plots

您不需要单独硬核每个数据集。您可以简单地调用 np.random.randint(low, high, (x, y, n)),其中 n 是 scans/trials 的编号。沿最后一个轴对它们求和意味着您将得到一个形状为 (x, y) 的数组。这可以通过 np.sum().

轻松完成

可以找到在子图中添加标题的方法here。总的来说,

import numpy as np
import matplotlib.pyplot as plt

np.random.seed(0)

sets = 6
data = np.random.randint(0, 2, (10, 20, sets))

def plot_data(data):
    total = np.sum(data, axis=-1)

    fig = plt.figure(figsize=(8, 10))
    ax1 = fig.add_subplot(211)
    ax2 = fig.add_subplot(212)   
    ax1.imshow(total, cmap= 'gray', interpolation= 'nearest')
    ax1.set_title('Image')
    # best way to flatten a numpy array
    ax2.hist(total.flatten())
    ax2.set_title('Histogram')
    plt.show()

plot_data(data)