2011-08-06 53 views
1

これは私の最初のプログラムです。私のプログラムでは面白いことがあります。プログラムは、特定のディレクトリで見つかったファイルから3列を読み込みます。次に、各ファイルのヒストグラムを計算し、2D-Histのようなものを作成するために結果を2次元マトリックスに追加します。matplotlibを使った対数2Dヒストグラム

私の難しさは、私がy軸のデータを対数スケールにし、そのデータをスケールに従って提示したいという私の3番目のプロットです。さらに、私は入力項目から "ゼロ"の項目を削除したいと思います。私はそのためnumpy.where(matrix)を使用しようとしましたが、それは本当にここ

が私のコードです...私が欲しいものをしなければわからない。:

#!/usr/bin/python 
# Filename: untitled.py 
# encoding: utf-8 

from __future__ import division 
from matplotlib.colors import LogNorm 
import matplotlib 
import numpy as np 
import matplotlib.pylab as plt 
import os 
import matplotlib.cm as cm 

def main(): 

    dataFiles = [filename for filename in os.listdir(".") if (filename[-4:]==".log" and filename[0]!='.')] 
    dataFiles.sort() 

    p = [] 
    matrix1 = [] 
    matrix2 = [] 
    matrix3 = [] 

    for dataFile in dataFiles: 
      p += [ eval(dataFile[11:16]) ] 
      data = np.loadtxt(dataFile, skiprows=7)[:,1:4] 

      matrix1 += [ data[:,0] ] 
      matrix2 += [ data[:,1] ] 
      matrix3 += [ data[:,2] ] 

    matrixList = [matrix1, matrix2, matrix3] 

    #make histograms out of the matrices 
    matrix1Hist = [ np.histogram(matrixColumn, bins=30, range=(np.min(np.where(matrix1 != 0)), np.max(matrix1)))[0] for matrixColumn in matrix1 ] 
    matrix2Hist = [ np.histogram(matrixColumn, bins=200, range=(np.min(np.where(matrix2 != 0)), np.max(matrix2)))[0] for matrixColumn in matrix2 ] 
    matrix3Hist = [ np.histogram(matrixColumn, bins=50, range=(np.min(np.where(matrix3 != 0)), np.max(matrix3)))[0] for matrixColumn in matrix3 ] 

    # convert the matrixHistogramsto numpy arrays and swap axes 
    matrix1Hist = np.array(matrix1Hist).transpose() 
    matrix2Hist = np.array(matrix2Hist).transpose() 
    matrix3Hist = np.array(matrix3Hist).transpose() 

    matrixHistList = [matrix1Hist, matrix2Hist, matrix3Hist] 

    fig = plt.figure(0) 
    fig.clf() 

    for i,matrixHist in enumerate([matrix1Hist, matrix2Hist, matrix3Hist]): 
      ax = fig.add_subplot(2, 2, i+1) 
      ax.grid(True) 
      ax.set_title('matrix'+str(i+1)) 
      if i < 2: 
        result = ax.imshow(matrixHist, 
             cmap=cm.gist_yarg, 
             origin='lower', 
             aspect='auto', #automatically span matrix to available space 
             interpolation='hanning', 
             extent= [ p[0], p[-1], np.floor(np.min(matrixList[i])), np.ceil(np.max(matrixList[i])) ] , 
            ) 

      elif i == 2: 
        result = ax.imshow(matrixHist, 
             cmap=cm.gist_yarg, 
             origin='lower', 
             aspect='auto', #automatically span matrix to available space 
             interpolation='hanning', 
             extent= [ p[0], p[-1], 1, np.log10(np.max(matrixList[i])) ] , 
             ) 


      ticks_at = [ 0 , abs(matrixHist).max()] 
      fig.colorbar(result, ticks=ticks_at,format='%1.2g') 


    plt.show() 


if __name__ == '__main__': 
    main() 
+0

するには、これはあなたの最初のPythonプログラムであることをあなたは確かにいますか? Pythonコードを見ると、かなりよく書かれているようですが、 'LogNorm'はインポートされていますが使用されていません。私はちょうど宿題をしましたか? ;-) – Brendan

答えて

3

質問の最初の部分については、あなたが持っています

  • プロットする前に結果にnp.log(my_array)を適用してください。
  • histプロットの軸を、前の質問Plot logarithmic axes with matplotlib in pythonと比較してスケールします。
  • normキーワードを使用してmatplotlib.colors.LogNormインスタンスを渡すことによって、pcolorおよびimshowのような2次元カラープロットをスケールします。すなわちimshow(my_array, cmap=mpl.cm.jet, norm=mpl.colors.LogNorm)

あなたの質問の後半部分については - 配列からゼロ値をフィルタリングについて - 試してみてください。

my_array = my_array[my_array != 0]

my_array != 0は、その後に使用されTrueFalse Sの論理的な配列を作成しますスライスしかし、これはおそらくあなたが望まない1次元の配列を返します。何か他のものに値を設定(および2D形状を維持する)、次(の値がNaNに設定されている)を使用...

my_array[my_array != 0] = np.NaN

+0

あなたの答えに感謝します。私はこのログの問題に対する解決策を見つけようとしたので、あなたがここで与えたリンクも見つけましたが、 'set_yscale( 'log') 'の解決策は残念ながら' imshow'では動作しません。 – Eagle

+1

ああ、「ヒストグラム」はカラープロットとして表示されますか?この場合、 'cmap'を対数的に変化するものに変更することができます。私は私の頭の上からこれをやる方法を知らないけど、それは可能です... – Brendan

関連する問題