如何在python中创建一个循环来输入变量?

时间:2017-10-10 20:56:30

标签: python numpy matplotlib subplot

我想通过制作某种可以通过变量名称的循环来修改这段代码,所以我不应该记下那些'如果data1不是None'部分?我还想知道是否有一种方法可以制作某种动态代码,函数的输入数量可能会以某种方式改变,例如,让我说我想输入100个不同的数据集,我可以& #39;在函数输入部分写下所有这些,我该怎么做? 另外,我怎么能为两个情节都加上标题呢?因为当我使用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

1 个答案:

答案 0 :(得分:0)

您无需单独核心每个数据集。您只需拨打np.random.randint(low, high, (x, y, n))n即可以扫描/试用次数。沿着最后一个轴对它们求和意味着你将得到一个形状为(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)