[Python][Gnuplot] PythonからGnuplotを使ってグモウスキー・ミラの写像を作図する
環境
Windows10 + Anaconda 4.8.1 + Python 3.7.6 + gnuplot 5.2(patchlevel 8)
はじめに
以前、PythonでMatplotlibを使ってグモウスキー・ミラの写像を作図したのだが、小さな点の打ち方が分からず、あまり綺麗な図にならなかった。
そのリベンジとして今回は、Matplotlibの代わりにGnuplotを使って作図してみる。
結果
ソースコード
import numpy as np from tqdm import tqdm import subprocess x0 = 0.1 y0 = 0.1 N = 1000000 FILENAME = 'gm.csv' def F(x, mu): return mu * x + 2 * (1 - mu) * x**2 / (1 + x**2) def func(x, y, alpha, sigma, mu): x_next = y + alpha * (1 - sigma * y**2) * y + F(x, mu) y_next = - x + F(x_next, mu) return (x_next, y_next) def calc(params): x, y = (x0, y0) with open(FILENAME, mode='w') as f: for _ in tqdm(list(range(N))): f.write('{:.8f}, {:.8f}\n'.format(x, y)) x, y = func(x, y, **params) return def plot(): cmd = '; '.join( [ r'set terminal png size 3840, 2880', r'set output \"test.png\"', r'set datafile separator \",\"', r'set xrange [-20.0:24.0]', r'set yrange [-14.5:12.5]', r'set noborder', r'set noxtics', r'set noytics', r'plot \"gm.csv\" using 1:2 notitle pt 7 ps 0.1', r'exit' ] ) cmd = 'gnuplot -e "{}"'.format(cmd) subprocess.run(cmd, text=True) return def main(): params = { 'mu': -0.8, 'alpha': 0.008, 'sigma': 0.05 } calc(params=params) plot() return if __name__ == '__main__': main()
解説
簡単にですが。
def calc():
~return
でグモウスキー・ミラの写像をN
回計算し、計算結果をgm.csv
(カンマ区切り)に出力。def plot():
~return
でGnuplotを使ってgm.csv
をプロット。- コマンドプロンプトで
gnuplot -e "command1;command2..."
と入力すると、gnuplotに一連のコマンドを実行させることができる。Pythonでからsubprocess.run
関数でこのコマンドを入力すればGnuplotを使うことができる(記事最下部の参考ページを参照のこと)。 - gnuplotに送るコマンドの中ではダブルクオーテーションをエスケープする必要があることに注意。
- 以下はgnuplotのコマンドの解説
set datafile separator ","
は、カンマ区切りのファイルを読み込むための設定。set noborder
で枠・軸を削除。set noxticks
とset noyticks
で目盛りを削除。plot "gm.csv" using 1:2 notitle pt 7 ps 0.1
でプロット。notitle
でlegendを削除。pt 7
でマーカーを丸に設定。ps 0.1
はマーカーサイズの設定。
参考ページ
PythonからGnuplotを使う方法を参考にさせていただいた。
auewe.hatenablog.com