2018年9月30日日曜日

gnuplot を python から使ってみる

python を使っていて、データ確認にグラフを使いたい時、自分は、 matplotlib を「ちょい」と使えないので、 gnuplot を 使えないか調べて(探して)みた。 で、 PyGnuplot というのが良さそうだったので、 試してみた。

サンプルを走らせると、画面表示が煩わしいのだが、 一旦 null へ向けておいて、作業後、ファイルにすることで、 すっきりした。


#!~/.pyenv/shims/python
"""Test for gnuplot."""
#

import sys

import numpy as np
import PyGnuplot as gp


def main():
    """Use gnuplot in python script."""
    #

    d_x = np.arange(1000)/20.0
    d_y1 = d_x-25
    d_y2 = d_y1*np.sin(d_x-25)

    # テンポラリのデータファイルを作成
    gp.s([d_x, d_y1, d_y2], filename='example.out')

    # terminal を null にしておく
    gp.c('set terminal pdf')
    gp.c('set output "/dev/null"')  # for win 'set output "nul"'

    # gnuplot で作業
    gp.c('set title "example.pdf"; set xlabel "x-axis"; set ylabel "y-axis"')
    gp.c('set yrange [-25:25]; set key center top')
    gp.c("plot 'example.out' u 1:2 w l t 'y=x-25")  # plot fist part
    gp.c("replot 'example.out' u 1:3 w l t 'y=(x-25)*sin(x-25)'")
    gp.c("replot 'example.out' u 1:(-$2) w l t 'y=25-x'")
    
    # 出力先を pdf ファイルに変更して、replot する
    gp.c("set output 'out.pdf'")
    gp.c('replot;')
    # gp.pdf('out.pdf') # export figure into a pdf file

    return


if __name__ == '__main__':
    main()
    sys.exit()
#

0 件のコメント:

コメントを投稿

Emacs の lsp の設定、なう(202310)

前回さらしてから、さらに1年。そう、3年めになる。 が、今回は一段と自信がない。 環境は、 Debian GNU/Linux 12 (bookworm) + emacs(29.1)。consult + company。 embark は未だに使ってない。 用途は、メモ と ...