l8l技術メモ

機械学習・深層学習・組込みとかのメモ

gnuplot, matplotlib メモ

本当にただのメモ書き。 どちらを使うかいつも悩む。gnuplotはプロットまでがはやいが、色や線種をいじるのが面倒(特に古いバージョンにおいて)。matplotlib は import ~ から始める必要がある。また、描けるグラフの種類は(知ってる限りでは)多く、ドキュメントも豊富。汎用性を重視するならば matplotlib か。プロットまで遅いのはエディタの snippet や matplotlib を内部で呼び出すスクリプトコマンドを書くなどで対応するなど。


gnuplot

基本

plot "file" u[sing] 1:($2 * 3) t "title" {wl | wp } ps 3
replot 2*x t "2x"
replot 3*x dashtype(dt) (3,1) # gnuplot>5.0

set key top left
set xlabel "xlabel" offset 0,-3 # offset は軸名の位置調整 
set bmargin 4 # offset 調節したことで文字が画面からはみ出たらこれでマージンを作成

set title "title 多くの場合タイトルはつけない"
set xlabel font "Arial,30"
set ylabel font "Arial,30"
set tics font "Arial,20"
set key font"Arial,20"

# save
set terminal pdfcairo enhanced size 4in,6in
set output "output.pdf"
replot
set termianl X11 ; set output

matplotlib

font まとめて plt.rcParams["font.size"] = 18

labelがはみ出る

まずは plt.tight_layout()
だめなら plt.subplots_adjust( left=0.2 ) # left は ylabel がはみ出る場合
https://matplotlib.org/api/_as_gen/matplotlib.pyplot.subplots_adjust.html

tex つかえる plt.plot( label=r"$x=\sqrt{2}$")
plt.legend( fontsize=12, loc="upper left" )
plt.tick_param( labelsize=14 )