1

I want to modify this code by making maybe some sort of loop that could go through the variable names so I should not write down that 'if data1 is not None' part? I was also wondering if there is a way that I could make some sort of dynamic code that the number of inputs into the function could change somehow, for example, let's say I want to input 100 different data sets, I can't write down all of them in the part of input for function, what should I do for that? Also, how could I put title for both of the plots? because when I use the plt.title(), it only shows the last 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)

Output should be:

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

Nikki
  • 195
  • 9
  • Welcome to StackOverflow. Please read and follow the posting guidelines in the help documentation. [Minimal, complete, verifiable example](http://stackoverflow.com/help/mcve) applies here. – Prune Oct 10 '17 at 21:23
  • Stick to asking one question per topic. Anyway, do the various `randomSet`s have the same amount of data points between them every time? And is there a reason to seed the RNPG between sets? And what's the point of `nscans`? – Reti43 Oct 10 '17 at 21:27
  • sorry about asking more than one question. All the data sets are made of 0 and 1 and have the same dimensions, they are basically collected from the same sample but some of the data are different between them because of noise or the inaccuracy of the device. nscans basically is a way to know how many times the data have been collected so I could use the loop for going through the list the same times. – Nikki Oct 10 '17 at 21:36

1 Answers1

0

You don't need to hardcore each dataset individually. You can simply call np.random.randint(low, high, (x, y, n)), with n being the number of scans/trials. Summing them along the last axis means you'll get an array with shape (x, y). This can be done trivially with np.sum().

The way to add a title in a subplot can be found here. Overall,

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)
Reti43
  • 9,656
  • 3
  • 28
  • 44