【Python】各モジュール,関数操作 逆引き表
■いろいろ
目的 | モジュール,メソッド |
---|---|
クリップボードにコピー | xerox.copy() |
モジュールのインポート元 | help(インポートしたモジュール名) |
フォルダの監視(作成,削除など) | watchdog. |
フォルダ内のファイル一覧取得 | glob.glob() |
フォルダ内のファイル一覧取得(拡張子指定) | glob.glob("*.***") |
ファイルの拡張子を取得 | os.path.splittext(フルパス) |
フルパスからファイル名のみを取得 | os.path.basename(フルパス) |
フルパスからフォルダ名のみを取得 | os.path.dirname(フルパス) |
プロセス正常終了時に実行 | import atexit, atexit.register(実行するもの) |
ディスプレイの解像度を取得(x方向) | ctypes.windll.user32.GetSystemMetrics(0) |
ディスプレイの解像度を取得(y方向) | ctypes.windll.user32.GetSystemMetrics(1) |
■matplotlib関連
目的 | モジュール,メソッド | |
---|---|---|
ウィンドウマネージャの取得(アクティブなウィンドウ) | plt.get_current_fig_manager() | |
ウィンドウマネージャの取得(figオブジェクトに対して) | fig.canvas.manager | |
ウィンドウサイズ,位置の取得 | geom = mgr.window.geometry(), x,y,dx,dy = geom.getRect() | |
ウィンドウサイズ,位置の設定 | mgr.window.setGeometry(x, y, dx, dy) | |
figureのサイズ設定 | plt.figure(figsize=(横,縦) | ※インチ指定なので注意 fig.get_dpi()でdpiを取得すると良い |
figureのサイズ設定 | fig.set_size_inches | ※インチ指定なので注意 fig.get_dpi()でdpiを取得すると良い |
各サブプロットのサイズ調整 | matplotlib.gridspec.Gridspec(総行数,総列数,width_ratios=,height_ratios=)をsubplot()の引数へ | |
各gridspecの周辺隙間調整 | gridspec.update(right=**,left=**,top=**,bottom=**) | |
Windowの名前変更 | figインスタンス.canvas.set_window_title("name") | |
Windowの初期位置(BackendがQtの時のみ | ||
figureの大タイトル設定 | fig.suptitle("***") | |
サブプロットのタイトル設定 | ax.set_title("***") | |
axesの背景色変更 | ax.get_axis_bgcolor("**") | |
axesの現在の色を取得 | ax.get_axis_bgcolor() | |
y軸範囲の設定 | ax.set_ylim[**,**] | |
x軸範囲の設定 | ax.set_xlim[**,**] | |
グラフ内にテキスト表示 | ax.text(x座標, y座標, str) | 座標はax内での座標になるので注意 |
グラフ外にテキスト表示 | fig.text(x座標, y座標, str) | 座標はfig内での座標になるので注意 |
凡例の表示 | ax.legend()※プロット引数でlabel = "**"を指定 | |
グリッドの表示 | ax.grid(True) | |
グリッドで主線を表示 | ax.grid(True, which="major") | |
グリッドで補助線を表示 | ax.grid(True, which="minor") | |
グリッドで主線・補助線を表示 | ax.grid(True, which="both") | |
グラフ枠を非表示 | ax.set_axis_off() | 外枠線,背景色,グリッド全て消える。プロットは可能 |
軸の色変更 | ax.spines['bottom'].set_color('red') | |
軸の幅変更 | ax.spines['bottom'].set_width(**) | |
軸ラベルの色を変更 | ax.xaxis.label.setcolor('r') | |
Tickラベルを非表示 | ax.tick_params(labelbottom="off",labelright="off") | |
Tickラベルの色を変更 | ax.tick_parms(axis = 'x', colors = 'red') | |
Tickラベルを指定 | ax.set_xticks([リスト]) | |
Tick軸を非表示 | ax.tick_params(bottom="off",right="off") | |
プロットのマーカー 中抜きの○ | ax.plot(...markerfacecolor="none") | ※中抜きの○はデフォルト設定なし |
マーカー,ラインの透過度 | (...alpah=0.5) | ※0で完全に透明, 1.0が非透過 |
マーカー種類 | (...marker="**") http://matplotlib.org/api/markers_api.html | |
マーカーサイズ | (...markersize="数値") | |
ライン種類 | (...linestyle="**")http://matplotlib.org/api/lines_api.html#matplotlib.lines.Line2D.set_linestyle | |
y2軸のプロット | ax1 = plot(...) ax2 = ax1.twinx() | ax1とax2をtwinx()で関連づける |
■numpy,scipy
目的 | モジュール,メソッド | |
---|---|---|
matファイルのロード | scipy.io.loadmat(ファイルパス) | |
複素数から位相取り出し | np.angle(**,True) Trueで戻り値が角度になる | |
最大値のindex | numpy.argmax(list or ndarray) | |
最小値のindex | numpy.argmin(list or ndarray) | |
極大値を求める | {} = scipy.signal.argrelmax() | |
極小値を求める | {} = scipy.signal.argrelmin() | |
ndarrayのデータ型変換(floatへ変換の例) | (np).astype('float') | インスタンスメソッド |
ndarrayのデータ型変換を調べる | (np).dtype | |
csvをscipy.arrayとして取り込む | scipy.genformtxt() | |
numpy同士の結合 | http://qiita.com/sotetsuk/items/d0e73afdcffdc8ac3e6b | |
■Pandas
目的 | モジュール,メソッド |
---|---|
データフレームからndarrayに変換 | df.as_matrix() |