2017-10-10 9 views
0

このコードを変更するには、変数名を調べるループを作成して、 'data1がNoneではない場合'という部分を書き留めてはいけません。私は、関数への入力数が何とか変わってしまうようなダイナミックなコードを作る方法があるかどうかも疑問に思っていました。たとえば、100個の異なるデータセットを入力したいとします。それらのすべてが機能のための入力の部分で、私はそれのために何をすべきですか? また、どのようにして両方のプロットにタイトルを付けることができますか? plt.title()を使用すると、最後のタイトルのみが表示されるためです。Pythonで入力変数を調べるためのループを作る方法は?

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

+0

ようこそStackOverflow。ヘルプドキュメントの投稿ガイドラインを読み、それに従ってください。 [最小、完全で検証可能な例](http://stackoverflow.com/help/mcve)がここに適用されます。 – Prune

+0

トピックごとに1つの質問をすることに固執する。とにかく、さまざまな 'randomSet'は毎回同じ量のデータポイントを持っていますか?セット間にRNPGを植え付ける理由はありますか? 'nscans'のポイントは何ですか? – Reti43

+0

申し訳ありませんが複数の質問をしています。すべてのデータセットは0と1で構成され、同じ次元を持ちますが、基本的に同じサンプルから収集されますが、デバイスのノイズや不正確さのためにデータの一部が異なります。 nscansは、基本的に、データが収集された回数を知る方法であり、ループを使用して同じ時間にリストを調べることができます。 –

答えて

0

あなたが個別に各データセットをハードコアにする必要はありません。 np.random.randint(low, high, (x, y, n))に電話してください。nはスキャン/試行の回数です。最後の軸に沿ってそれらを合計すると、shape(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) 
+0

ありがとうございました。それは完璧です。 –

関連する問題