M2Plot+のCUI操作#
M2Plot+の機能をPythonのコマンドインターフェースや、スクリプト上から実行するためのコマンドも準備されている。例えば以下のようなことをしたい場合である。
GUIを表示して、コマンドインターフェースで制御したい( M2PlotPlusCui 参照)
GUIを表示させずに、スクリプト内でプロットの画像ファイルを作成したい( U2IFInDark 参照)
Jupyterで使いたい( U2IFJupyter 参照)
スクリプト内でデータをスライスしたい( スライスをコマンドで実行 参照)
CUIでの制御には上記で述べたように、行いたい作業によっていくつかのコマンドに分かれる。 ただし全機能を制御することはできないことに注意。例えば M2PlotPlusCui ではコマンドで初期設定の後、GUIで詳細な制御することを想定している。
本稿では以下の項目に分けて説明する。
-
GUIを表示して、そのGUIをコマンドで制御する
-
GUIは表示せず(Xサーバーなどは不要で)コマンドで制御する
-
Jupyter notebookで使用する
-
スライスを実行する(M2Plot+内で使用されている関数を直接呼び出す)
なお、 U2IFInDark と U2IFJupyter のコマンド仕様はほぼ同じ(Jupyter用に表示用のコマンドが一つ追加されるだけ)である。
M2PlotPlusCui#
ここではGUIを表示しコマンドで制御する方法を述べる。
まず、M2Plot+のCUIモジュールである M2PlotPlusCui を読み込み起動すると、M2Plot+のGUIのメインパネルが表示される。
>>> import uGao.M2PlotPlusCui as M2P
>>> p = M2P.M2PlotPlusCui()
あらかじめデータが作成されているなら、起動時に指定できる。参考例では ECA をデータとする。
>>> p = M2P.M2PlotPlusCui(ECA)
もし起動後にデータを与える場合やデータの変更を行う場合は以下のようなコマンドを用いる。
>>> p.ChangeData(ECA2)
データを与えられたらプロッタを開く(GUIの Show ボタンと同じ機能)
>>> p.ShowData()
なお、データの軸を選択してプロットする場合(GUIのメインパネルの X axis や Y axis の設定)は以下のように指定する。
# "Qx"と"Qy"がそれぞれ X axis と Y axis とする場合
>>> p.ShowData("Qx", "Qy")
また、強度方向のスケールを変更することや、X軸Y軸のスケールを変更することも可能である。詳細は次章で述べる。
>>> p.SetIntScale(0, 200) # 強度の範囲を0-200で表示
>>> p.SetXScale(-5, 5) # X軸の範囲を-5 から 5に設定
>>> p.SetYScale(0.0, None) # Y軸の範囲を0.0から最大値までに設定
M2PlotPlusCuiのコマンド詳細#
以下にM2PlotPlusCuiで使用できるコマンド(メソッド)を示す。
実際にスクリプトとしてM2PlotPlusCuiのコマンドを使用する場合は、下記のようになる。
import uGao.M2PlotPlusCui as M2P
# ECA にデータが入っているとする
p = M2P.M2PlotPlusCui(ECA) # 夕顔のGUIを起動してデータを与える
p.ShowData() # プロットウィンドウにプロットする
p.SetLabels("Qa [rlu]", "Qb [rlu]") # X軸とY軸のラベルを設定
p.SetIntScale(0.1, 500) # 強度の範囲を0.1-500に
p.SetIntLog(True) # 強度をログスケールに
del p
上記の p.XXX のXXXの部分がコマンド(メソッド)となる。以下に主に使用可能なメソッドをリファレンスとして記述する。
メソッド |
説明概要 |
|---|---|
ShowData(xkey, ykey, rkey, r_min, r_max) |
読み込ませたデータを2Dプロッタで表示する |
SetIntScale(min_v, max_v) |
強度のスケールを指定する。どちらかのパラメータとしてNoneを与える とAuto Scale時の値となる |
SetXScale(min_v, max_v) |
X軸のスケールを指定する。どちらかのパラメータとしてNoneを与える とAuto Scale時の値となる |
SetYScale(min_v, max_v) |
Y軸のスケールを指定する。どちらかのパラメータとしてNoneを与える とAuto Scale時の値となる |
SetIntScaleAuto() |
強度のスケールをAutoにする(範囲の値もAutoの値となる) |
SetXScaleAuto() |
X軸のスケールをAutoにする(範囲の値もAutoの値となる) |
SetYScaleAuto() |
Y軸のスケールをAutoにする(範囲の値もAutoの値となる) |
SetLabels(xlabel, ylabel) |
X軸およびY軸のラベルを指定する。 |
SetUnits(xunit, yunit) |
X軸およびY軸の単位(プロッタ上で表示)を指定する |
SetTitles(title, comment) |
プロッタのタイトル(1行目)とコメント文(2行目)を指定する |
SetSmoothing(isSmooth, windowVal, timesVal) |
強度のスムージングを行う |
SetWindowSize(width, height) |
プロットウィンドウ自体のサイズを指定する |
SetIntLog(flag) |
強度のログスケールを設定する |
SetXLog(flag) |
X軸方向のログスケールを設定する |
SetYLog(flag) |
Y軸方向のログスケールを設定する |
SetWindowTitle(title) |
プロットウィンドウのタイトルを設定する |
SaveAsText(filename, dQ, isIgnoreMask, maskinfo) |
プロット中のデータをテキストファイルに保存する |
SetSliceMethod(isAve) |
スライスの強度計算が平均値か積算値かを指定する |
Close() |
M2PlotPlusを閉じる(パネル・ウィンドウ共に) |
以下に詳細なリファレンス(引数の説明を含む)を示しておく。
- class M2PlotPlusCui(data=None, closeFlag=True)#
Class to control M2PlotPlus on CUI
- Parameters:
data (ElementContainerArray/Matrix) – Data to be plotted.
closeFlag (bool) – Flag to close M2PlotPlus when the pearent class closes.
- ChangeData(data=None)#
Send data to M2PlotPlus
- Parameters:
data (ElementContainerArray/Matrix) – Data to be plotted.
- ShowData(xkey='-', ykey='-', rkey='-', r_min=0.0, r_max=0.0)#
Plot data with given axes data set defined by keys
- Parameters:
xkey (str) – Key of vector in data for X axis
ykey (str) – Key of vector in data for Y axis
rkey (str) – Key of 3rd vector in data for DetectMap mode
r_min (str) – range of 3rd vector for DetectorMap mode
r_max (str) – range of 3rd vector for DetectorMap mode
- SetLabels(xlabel='-', ylabel='-')#
Set Labels for axes
- Parameters:
xlabel (str) – Label of X-axis
ylabel (str) – Label of Y-axis
- SetUnits(xunit='-', yunit='-')#
Set Units for axes
- Parameters:
xunit (str) – Unit for X-axis
yunit (str) – Unit for Y-axis
- SetTitles(title='-', comment='-')#
Set Titles
- Parameters:
title (str) – Title for 2d plotting
comment (str) – Comments for 2d plotting
- SetSmoothing(isSmooth=False, windowVal=1, timesVal=1)#
Set Smoothiing
- Parameters:
isSmooth (bool) – Affect Smoothing or not
windowVal (int) – Window Size for smoothing
timesVal (int) – The times of smoothing
- SetWindowSize(width, height)#
Set Window Size
- Parameters:
width (int) – window width
height (int) – window height
- SetIntLog(flag=True)#
Set log scale on Intensity
- Parameters:
flag (bool) – True:On, False:Off
- SetXLog(flag=True)#
Set log scale on X axis
- Parameters:
flag (bool) – True:On, False:Off
- SetYLog(flag=True)#
Set log scale on Y axis
- Parameters:
flag (bool) – True:On, False:Off
- SetIntScale(min_v=None, max_v=None)#
Set scale range for Intensity. When not given both arguments,like SetIntScale(), Auto scale .
- Parameters:
min_v (float) – minumum of range / (None) use of auto scale value
max_v (float) – maximum of range / (None) use of auto scale value
- SetXScale(min_v=None, max_v=None)#
Set scale range for X axis. When not given both arguments,like SetXScale(), Auto Xscale turns on.
- Parameters:
min_v (float) – minumum of range
max_v (float) – maximum of range
- SetYScale(min_v=None, max_v=None)#
Set scale range for Y axis. When not given both arguments,like SetYScale(), Auto Yscale turns on.
- Parameters:
min_v (flaot) – minumum of range
max_v (float) – maximum of range
- SetIntScaleAuto(flag=True)#
Set auto scale mode on the Intensity. (added Jan. 9th 2026)
- Parameters:
flag (bool) – True: set auto scale mode for the intensity
- SetXScaleAuto(flag=True)#
Set auto scale mode on the X axis. (added Jan. 9th 2026)
- Parameters:
flag (bool) – True: set auto scale mode for the X axis
- SetYScaleAuto(flag=True)#
Set auto scale mode on the Y axis. (added Jan. 9th 2026)
- Parameters:
flag (bool) – True: set auto scale mode for the Y axis
- SetCurrentKey(key):
Set a current key
- Parameters:
key (str) – key
- SetWindowTitle(title)#
Set Window Title
- Parameters:
title (str) – window title
- SaveAsText(filename, dQ=0.0, isIgnoreMask=False, maskinfo=[False, 0.0])#
Save plotted data to a text file
- Parameters:
filename (str) – path to file to be saved
dQ (flaot) – delta Q value for powder data with inelastic scattering data
isIgnoreMask (bool) – Flag to ignore mask values in plotting data on writing text file
maskinfo (list) – [<bool>, <float>] <bool> is flag to use given mask values (<float>) insteed of original ones
- SetSliceMethod(isAve)#
Set Slice method to add data
- Parameters:
isAve (bool) – True:average mode, False:summation mode
- Close()#
Close M2PlotPlus
U2IFInDark#
ここではGUIを表示することなくコマンドで制御する方法を述べる。 例えば、GUIを表示せずにプロットを作成し画像ファイルとして保存するスクリプトを作成する場合に用いる。
以下のように使用する。
import uGao.U2IFInDark as UIF
# sliced_ecaにデータが入っているとする
p = UIF.U2IFInDark(sliced_eca) # コマンドの準備とデータ指定
p.SetKeys("ax3", "ax4", "Intensity") # 横軸、縦軸のキー("ax1", "ax2", "ax3", "ax4")、および強度のキー
p.SetData() # データの読み込み(SetKeysの値を使用するので順番はこの位置)
p.SetRange(False, 0.0, 40.0) # 強度の範囲指定(最初のFalseはAuto Scaleはしないの意味)
p.SetXScale(False, None, 4.0) # 範囲の値がNoneならAuto Scale時の値(データの最大・最小値)を意味する
p.SetTitles("Test Data", "SubTitle") # TitleとSubTitle
p.SetLabels("Qa (rlu)", "hw (meV)") # 軸のラベル
p.SaveFile("TestData_A-2.png") # 画像ファイルとして保存
del p
U2IFInDarkのコマンド詳細#
以下にU2IFInDarkで使用できるコマンド(メソッド)を示す。
メソッド |
説明概要 |
|---|---|
SetKeys(xkey, ykey, intkey=None, rkey=None) |
軸と強度に使用するキーを指定する。 これはSetDataの前に行う必要がある |
SetData(data) |
データ(ElementContainerArray/ -Matrix)を与える。 これはSetKeysの後で与える必要がある。 |
SetIntRange(range0, range1) |
3次元データのTOF方向の範囲(積分範囲)を指定する |
SetTitles(title, comment) |
プロッタのタイトル(1行目)とコメント文(2行目)を指定する |
SetTransPosition(flag) |
軸の転置(入れ替え)を行う |
SetXScale(autoFlag, r_min=None, r_max=None) |
X軸の範囲(スケール)の設定(値がNoneならAuto Scale値) |
SetYScale(autoFlag, r_min=None, r_max=None) |
Y軸の範囲(スケール)の設定(値がNoneならAuto Scale値) |
SetLabels(xlabel, ylabel) |
軸のラベルの設定を行う |
SetHGrid(grid, color=None, interval=None) |
プロットの横方向のグリッドの設定を行う |
SetVGrid(grid, color=None, interval=None) |
プロットの縦方向のグリッドの設定を行う |
SetColorMap(cmap) |
プロットのカラーマップを指定する |
SetSmooth(flag, times=1) |
スムージングの設定を行う |
SetSmoothing(isSmooth=False, windowVal=1, timesVal=1) |
|
SetBankSeparator(flag=False) |
DetectorMapモード時のバンクセパレータの有無を指定する |
SetIntRange(autoFlag, r_min=None, r_max=None) |
強度のスケールを指定する。どちらかのパラメータとして Noneを与えるとAuto Scale時の値となる |
SetRange(autoFlag, r_min=None, r_max=None) |
|
SetLog(zlog) |
強度のログスケールの設定 |
SetFigureSize(px, py) |
保存する画像ファイルのサイズを設定する |
SaveFile(filename) |
画像ファイルの保存を行う |
SaveAsText(filename) |
プロットデータをテキストで出力する |
以下に詳細なリファレンス(引数の説明を含む)を示しておく。
- U2IFInDark(data=None, orderNo=1, sizeX=7.0, sizeY=7.0, dpi=100):
Class to control U2IFInDark on CUI
- Parameters:
data (ElementContainerArray/Matrix) – Data to be plotted.
orderNo (int) – Plot number
sizeX (float) – size along X axis of the figure to be created
sizeY (float) – size along Y axis of the figure
dpi (float) – dot per inch of the figure
- SetKeys(xkey, ykey, intkey=None, rkey=None)#
Set keys in given data for axes used to plot
- Parameters:
xkey (str) – Key code to be used for X axis
ykey (str) – Key code to be used for Y axis
intkey (str) – Key code to be used for intensity
rkey (str) – Key code to be used for Y axis
- SetData(data=None)#
Set data. When data is given at beginning , data argument is not required.
- Parameters:
data (ElementContainerArray/Matrix) – Data to be plotted.
- SetIntRange(range0=None, range1=None)#
Set the integration range of tof axis to calcurate the intensity on 2D plot with 3D data(ElementCotnainerMatrix).
- Parameters:
range0 (float) – minumum of range / (None) use of auto scale value
range1 (float) – maximum of range / (None) use of auto scale value
- SetTitles(title='-', comment='-')#
Set Titles
- Parameters:
title (str) – Title for 2d plotting
comment (str) – Comments for 2d plotting
- SetTransPosition(flag)#
Swap X-axis and Y-axis in the plot
- Parameters:
flag (bool) – True do swap / False not swap
- SetXScale(autoFlag, r_min=None, r_max=None)#
Set scale range for X axis.
- Parameters:
autoFlag (bool) – True means the auto scale on X axis
r_min (flaot) – minumum of range when autoFlag is False / None means auto scale
r_max (float) – maximum of range when autoFlag is False / None means auto scale
- SetYScale(autoFlag, r_min=None, r_max=None)#
Set scale range for Y axis.
- Parameters:
autoFlag (bool) – True means the auto scale on Y axis
r_min (flaot) – minumum of range when autoFlag is False / None means auto scale
r_max (float) – maximum of range when autoFlag is False / None means auto scale
- SetLabels(xlabel, ylabel)#
Set Labels for axes
- Parameters:
xlabel (str) – Label of X-axis
ylabel (str) – Label of Y-axis
- SetHGrid(grid, color=None, interval=None)#
Set horizontal grid lines
- Parameters:
grid (bool) – True means turn the grid line on
color (int) – 0:White, 1:Yellow 2:Red, 3:Black, 4:Blue, 5:Green, 6:Cyan, 7:Purple
interval (int) – the interval value between grids
- SetVGrid(grid, color=None, interval=None)#
Set vertical grid lines
- Parameters:
grid (bool) – True means turn the grid line on
color (int) – 0:White, 1:Yellow 2:Red, 3:Black, 4:Blue, 5:Green, 6:Cyan, 7:Purple
interval (int) – the interval value between grids
- SetColorMap(cmap)#
Set color map on the plot
- Parameters:
cmap (str) – the name of the color map [‘Default’, ‘autumn’, ‘winter’, ‘gray’, ‘haxby’, ‘terrain’, ‘turbo’]
- SetSmoothing(isSmooth=False, windowVal=1, timesVal=1)#
Set Smoothiing
- Parameters:
isSmooth (bool) – Affect Smoothing or not
windowVal (int) – Window Size for smoothing
timesVal (int) – The times of smoothing
- SetBankSeparator(flag=False)#
Turn on/off the bank separator on the plot when given data is 3D(ElementContainerMatrix).
- Parameters:
flag (bool) – True means that the bank separators are shown
- SetIntRange(autoFlag, r_min=None, r_max=None)#
Set the scale range of intensity axis.
- Parameters:
autoFlag (bool) – True means the auto scale on the intensity axis
r_min (flaot) – minumum of range when autoFlag is False / None means auto scale
r_max (float) – maximum of range when autoFlag is False / None means auto scale
- SetRange(autoFlag, r_min=None, r_max=None)#
Set the scale range of intensity axis.
- Parameters:
autoFlag (bool) – True means the auto scale on the intensity axis
r_min (flaot) – minumum of range when autoFlag is False / None means auto scale
r_max (float) – maximum of range when autoFlag is False / None means auto scale
- SetLog(flag=True)#
Set log scale on Intensity
- Parameters:
flag (bool) – True:On, False:Off
- SetFigureSize(px, py)#
Set the size of the plot figure
- Parameters:
px (float) – size of X axis of the plot [cm]
py (float) – size of Y axis of the plot [cm]
- SaveFile(filename)#
Save the plot figure file
- Parameters:
filename (str) – the path of the file to be saved
- SaveAsText(filename)#
Save data into the text file
- Parameters:
filename (str) – the path of the file to be saved
U2IFJupyter#
Jupyter notebookでM2Plot+を利用するために用意されている。
U2IFInDark と同じコマンドが使える。 唯一、Jupyter notebook上にプロットを表示するためのコマンド Show が追加されるだけである。
以下のように使用する。
import uGao.U2IFInDark as UIF
# sliced_ecaにデータが入っているとする
p = UIF.U2IFInDark(sliced_eca) # コマンドの準備とデータ指定
p.SetKeys("ax3", "ax4", "Intensity") # 横軸、縦軸のキー("ax1", "ax2", "ax3", "ax4")、および強度のキー
p.SetData() # データの読み込み(SetKeysの値を使用するので順番はこの位置)
p.SetRange(False, 0.0, 40.0) # 強度の範囲指定(最初のFalseはAuto Scaleはしないの意味)
p.SetTitles("Test Data", "SubTitle") # TitleとSubTitle
p.SetLabels("Qa (rlu)", "hw (meV)") # 軸のラベル
p.Show() # Jupyter上でプロットする
del p
U2IFJupyter用追加メソッド |
説明概要 |
|---|---|
Show() |
Jupyter notebookでプロットを表示する |
スライスをコマンドで実行#
M2Plot+で行っているスライス作業をスクリプトなどに埋め込んで実行したい場合は、万葉ライブラリのスライスコマンド( Manyo.MLF.MlfArraySlicer )を利用する。このコマンドはM2Plot+内部でも使用されている汎用的なものである。
以下に2次元データ(ElementContainerArray)をスライスし、結果をMPlotで表示するスクリプトの例を示す。
# ECAにM2Plot+で表示できるデータが入っているとする
# MlfArraySlicerによるスライス手順の概要
# (1)スライスしたい軸と領域を指定
# (2)指定領域をどちらの軸に積分(射影)するかを指定しスライスデータを作成
# データ内のどの数列をスライス時の横軸と縦軸にするか決めるためのXkeyとYkeyを指定する
# これらはM2Plot+のメインパネルの"Plot Data Area"(一番上)のプルダウンメニューの文字列と同じ
Xkey = "Qx"
Ykey = "Qy"
# スライス対象領域
x_min = -0.5
x_max = 0.5
y_min = -0.02
y_max = 0.02
# スライス強度を平均するか積算するか
isAve = True
# スライスコマンドの準備(データと軸の指定も)
import Manyo.MLF as mm
ss = mm.MlfArraySlicer(ECA)
ss.SetAxes(Xkey, Ykey)
# スライス範囲を指定
ss.ClearClipRegion()
ss.SetClipRegion(x_min, y_min, x_max, y_max)
# X軸に沿ってスライス(Y軸方向は積分)
# 戻り値はElementContainerArray(複数の領域を指定できるため)
ret = ss.CutAlongX(isAve)
# Y軸に沿ってスライス(X軸方向は積分)する場合(上で指定したスライス対象領域に注意)
# ret = ss.CutAlongY(isAve)
# 最初の範囲のスライスを取り出す(範囲は複数指定できるため)
EC_alongX = ret.Put(0)
# スライスデータをMPlotで表示
import uGao.MPlot as mp
p1 = mp.MPlot(EC_alongX)