0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

【GMT】psxyコマンドを使ってグラフを描いてみた

Last updated at Posted at 2021-11-07

概要

GMT(GMT6を想定)のpsxyコマンドを用いてグラフを作成。
元々GMTは地図向けのツールだが、グラフ作成も可能。
グラフ作成ならPythonのmatplotlibなどもあるが、GMTなら記述量が少なく綺麗なグラフが描ける。
シェルベースなので、ターミナル直書きでも使えるのも良いところ。

psxyのオプション

こんな感じのオプションを付ける必要があります。
オプションの詳細はQiitaでは割愛します。

R, B, Jオプションは必須。
GMTの仕様上、追記する場合はK, Oオプションも必要。
WオプションとGオプションの共存は難しい。

bash
gmt psxy ${データ} -R${描画範囲} -B${軸関連の設定} -J${図法・描画サイズ} -W${線の太さ・色} -S${挿入図形の種類・大きさ} -G${図形の塗りつぶし色} -K -O >> ${保存する画像のパス}

グラフの例

こんなグラフたちができます。
徐々に難易度が上がり、変数は上から引き継いでいます。

これから使うデータパスはこれらを指しています。

psxy.sh
txtData=./data/data.txt
csvData=./data/data.csv

簡単なグラフ

基本的なグラフです。
実質1行。

psxy.sh
img=./img/test1.ps
range=1/5/0/10
base=a1f1g1/a2f3g4
gmt psxy $txtData -R$range -B$base -Jx3/2 -P > $img

image

タイトル・軸ラベル追加

Bオプションに軸ラベルを示す「: :」やタイトルを示す「:. :」を追加しました。他は一緒。

psxy.sh
img=./img/test2.ps
base=a1f1g1:"x"::."title":/a2f3g4:"y":
gmt psxy $txtData -R$range -B$base -Jx -P > $img

image

軸ラベルを一部のみ

Bオプションに東西南北を示す「SWne」を追記。
小文字にすれば詳しいところは描かれなくなる。

psxy.sh
img=./img/test3.ps
base=a1f1g1:"x"::."title":/a2f3g4:"y":SWne
gmt psxy $txtData -R$range -B$base -Jx -P > $img

image

線に色を追加

Wオプション登場。太さと色を指定可能。
色はRGBでも16進数でも可能。

psxy.sh
img=./img/test4.ps
lineColor=0/128/0
gmt psxy $txtData -R$range -B$base -Jx -W1,$lineColor -P > $img

image

座標に図形を追加

線と図形を同時に描けないので2行に。
ScはCircleのCで数値は図形サイズ。形はSt(Triangle)など何種類かある。
追記なので、K, Oオプションと出力を示す**>>>**を考慮する形に。

psxy.sh
img=./img/test5.ps
circleColor=green
gmt psxy $txtData -R$range -B$base -Jx -W1,$lineColor -P -K > $img
gmt psxy $txtData -R -B -J -Sc0.2 -G$circleColor -O >> $img

image

軸を複数に

Bオプションを上手く使って2軸描画も可能。
Jオプションを使って描画スケールは良い形にしてあげてください。

psxy.sh
img=./img/test6.ps
data2Color=red
data2Range=1/5/0/100
data2Base=a1g1/a20f10:"y2":E
gmt psxy $txtData -R$range -B$base -Jx -W1,$lineColor -P -K > $img
gmt psxy $txtData -R -B -J -Sc0.2 -G$circleColor -K -O >> $img
gmt psxy $csvData -R$data2Range -B$data2Base -Jx3/0.2 -W1,$data2Color -P -K -O >> $img
gmt psxy $csvData -R -B -J -St0.2 -G$data2Color -O >> $img

image

凡例追加

凡例は全く説明してませんが、pslegendでできます。
いつかちゃんと説明します。

psxy.sh
img=./img/test7.ps
frameColor=0/0/255
backColor=#FFFFFF
legRange=1/18.5/1/1.2
gmt psxy $txtData -R$range -B$base -Jx3/2 -W1,$lineColor -P -K > $img
gmt psxy $txtData -R -B -J -Sc0.2 -G$circleColor -K -O >> $img
gmt psxy $csvData -R$data2Range -B$data2Base -Jx3/0.2 -W1,$data2Color -P -K -O >> $img
gmt psxy $csvData -R -B -J -St0.2 -G$data2Color -K -O >> $img
gmt pslegend -R -J -Dx$legRange -F+p1,blue+gwhite -O <<EOF >> $img                  
S 0 c 0.2 green black 0.2 y                                                         
S 0 t 0.2 red black 0.2 y2                                                          
EOF       

image

まとめ

GMTは地図だけじゃないぞ!と言いたかっただけです。
2軸グラフまで理解できれば、グラフ作成には困らないのではないでしょうか。
これだけの記述量で実現できるのは嬉しいです。

その他

GitHubに今回の内容に対応するソースコードを置いています。
実験したい方は使ってみてください。

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?