ぱたへね

はてなダイアリーはrustの色分けができないのでこっちに来た

F#でグラフ(チャート)を書いてみた。

2年に1回くらい思い出したようにF#を始める人です。

matplotlibがあるからPythonから離れられない事に気がつき、グラフの書き方を調べました。

www.apress.com

Expert F#を見ながら FSharp.Chartingを使えば、それなりに簡単にグラフ書けました。やったね。

f:id:natsutan:20200613131319p:plain

f:id:natsutan:20200613131334p:plain

以下、ソース。

open FSharp.Charting
let path = @"C:\home\myproj\study_fs\expertfs4\chap10\fig\"

let drawPoint =
    let rnd = System.Random()
    let rand() = rnd.NextDouble()
    
    let randomPoints = [for i in 0 .. 1000 -> 10.0 * rand(), 10.0 * rand() ]
    let chart =  Chart.Point randomPoints
    let output_file = path + @"point.png" 
    Chart.Save output_file chart
        
    printf "%s\n"  output_file

let drawCombileChart =
    let rnd = System.Random()
    let rand() = rnd.NextDouble()
    let randomTrend1 = [for i in 0.0 .. 0.1 .. 10.0 -> i, sin i + rand()]
    let randomTrend2 = [for i in 0.0 .. 0.1 .. 10.0 -> i, sin i + rand()]
    let c1 = Chart.Line(randomTrend1, Title = "Random Trend")
    let chart = Chart.Combine [c1 ; Chart.Line randomTrend2]
    let output_file = path + @"trend.png"    
    Chart.Save output_file chart
    
    printf "%s\n"  output_file


[<EntryPoint>]
let main argv =
    drawPoint |> ignore
    drawCombileChart |> ignore

    0 // return an integer exit code