4
1

More than 5 years have passed since last update.

Crystalでgnuplot実行するの作ったった

Last updated at Posted at 2018-02-12

Crystalは実行速度めちゃくちゃ早くて計算向きなのに、awesome crystalみてもグラフ描出できるツールがないので、怒って超シンプルなのを作りました。
nuplot

(2018/3/12) 追記
(Array).to_gp から to_gp(Array) に変更しました。(bird1079s様ありがとうございました)

使い方

readmeの通りです

require "nuplot"

sin.png

Gnuplot.plot do |s|
  s << "plot sin(x)"
end

scatter2d.png

x = Array(Float64).new(100){ rand(10.0..20.0) }
y = Array(Float64).new(100){ rand(100.0..200.0) }

Gnuplot.plot do |s|
  s << "set title 'SCATTER'"
  s << "plot '-' pt 6 lt rgb 'red' t 'circle'"
  s << to_gp([x, y])
end

lines.png

memo = 0
x = Array(Float64).new(100,0.0)

100.times do |i|
  memo = memo + rand(-0.9..1.0)
  x[i] = memo
end

Gnuplot.plot do |s|
  s << "set title 'LINES'"
  s << "plot '-' with lines title 'x'"
  s << to_gp(x)
end

scatter3d.png

x = Array(Float64).new(1000){ rand(-10.0..10.0) }
y = Array(Float64).new(1000){ rand(-10.0..10.0) }
z = Array(Float64).new(1000){ |i| x[i]**2 + y[i]**2 }

Gnuplot.plot do |s|
  s << "set title 'SCATTER 3D'"
  s << "splot '-' pt 6 t 'z=x^2+y^2'"
  s << to_gp([x,y,z])
end

感想など

見ての通り生成した文字列の配列 s をgnuplotに投げ込むだけの超単純なツールです。けれども他にCrystalでグラフを描出するツールが見当たらないので、こんなもんでも需要はあるかなと思います。Arrayが入れ子になってる場合の型判定は難しいですね。きちんとできていません。

僕はこれを読んだ誰かが実用的なグラフ描出ツールを作ってくれるのを楽しみにしています。

4
1
5

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
4
1