2017-02-08 7 views
0

私は、Benjamin Rootの書籍「Matplotlibを使った対話型アプリケーション」のチュートリアルに従って、matplotlibイメージ軸のformat_coord()メソッドをサブクラス化しました。できます。私はmain.pyがデータポインタをインポートし、マウスを画像の上に動かすときに表示されるインタラクティブな数字を変更するためにそれを使用するguiアプリケーションを持っています。matplotlibでsub_classedのformat_coordにparamを渡しました

from gui_stuff.datapointer import DataPointerLeftCart 

し、それを呼び出して使用される:

self.ax_lhs = self.f_lhs.add_subplot(111,projection = 'sp_data_pointer') 

別のファイルに次のようにDataPointerLeftCartためのストリッピングコードが定義されている:

import matplotlib.projections as mproj 
import matplotlib.transforms as mtransforms 
from matplotlib.axes import Axes 
import matplotlib.pyplot as plt 


#Our own modules 
from operations.configurations import configParser 

class DataPointerLeftCart(Axes): 
    name = 'sp_data_pointer' 
    def format_coord(self, y, z): 
     configparams = configParser(ConfigFile=configfilename)   
     centreY = float(configparams['CENTRE_Y'])#units are pixels 
     scale = float(configparams['SCALE'])#nm^-1 per pixel 

    new_qy = (y-(centreY))*scale 

    return "Qy: %f (nm^-1)" % (new_qy) 

mproj.projection_registry.register(DataPointerLeftPol) 

のConfigParser()関数でありますテキストファイルを読み込んで、プログラムのさまざまな重要な構成番号を持つ辞書を作成します。もともとこれは引数がなく、configParserはテキストファイルの場所を指定しましたが、最近私はプログラム全体を変更して、設定ファイルのローカルコピーを指定しました。これは私にconfigfilename引数を渡せるようにするために必要です。しかし、私はこれを行う方法について混乱しています。 configfilenameはmain.pyから来なければなりませんが、ここではadd_subplotの引数として 'sp_data_pointer'という名前だけを与えます。

どこにもありませんので、これは私がクラスのインスタンスと、おそらく「のinit」メソッドは、私はサブクラス化しています軸内部の世話をされて作成する場所(目に見えて、私のコード内で)私を混乱さ。誰かが私を動かすための原理や汚れた回避策を教えてもらえますか(どちらもお勧めです!)

答えて

0

私はちょうどここで推測していますが、実際にはformat_coordの方法はAxes初期化。これにより、作成後にconfigfilenameを設定する可能性が開かれます。この目的のために、configfilenameにクラス変数を設定し、それに対してセッターを定義することができます。

class DataPointerLeftCart(Axes): 
    name = 'sp_data_pointer' 
    configfilename = None 

    def format_coord(self, y, z): 
     configparams = configParser(ConfigFile=self.configfilename)   
     centreY = float(configparams['CENTRE_Y'])#units are pixels 
     scale = float(configparams['SCALE'])#nm^-1 per pixel 
     new_qy = (y-(centreY))*scale 
     return "Qy: %f (nm^-1)" % (new_qy) 

    def set_config_filename(self,fname): 
     self.configfilename = fname 

作成後にセッターを呼び出します。

self.ax_lhs = self.f_lhs.add_subplot(111,projection = 'sp_data_pointer') 
self.ax_lhs.set_config_filename("myconfigfile.config") 

私はちょうどフォーマットを設定するためのAxesをサブクラス化について少し奇妙な感じを認めざるを得ません。

ので、代替はAxesをサブクラス化するのではなく、main.py内format_coordを使用しない可能性があり:

from operations.configurations import configParser 
configfilename = "myconfigfile.config" 

def format_coord(y, z): 
    configparams = configParser(ConfigFile=configfilename)   
    centreY = float(configparams['CENTRE_Y'])#units are pixels 
    scale = float(configparams['SCALE'])#nm^-1 per pixel 
    new_qy = (y-(centreY))*scale 
    return "Qy: %f (nm^-1)" % (new_qy) 

ax_lhs = f_lhs.add_subplot(111) 
ax_lhs.format_coord = format_coord 
+0

おかげで私は、最初の方法を使用し、それが魅力のように働きました! – MikeS

関連する問題