Haskell에서 그림 그리기 (gnuplot) -사용 예 -
Haskell에서 그래프 작성
Haskell에서 놀고 있다면 아무래도 그래프를 출력하고 싶어집니다 (이 기사의 그래프는 모두 다이어그램 그래프입니다). 대표적인 그래프 작성의 방법으로서 Chart 와 gnuplot 가 있습니다만, 이번은 Gnuplot 를 소개합니다.
gnuplot을 사용하는 이점은 설치가 간단하다는 것입니다. 예를 들어 우분투라면
sudo apt-get install gnuplot
cabal install gnuplot
에서 설치할 수 있습니다.
사용할 때도
import Graphics.Gnuplot.Simple
로 끝납니다.
이전에는 사용예와 해설 양쪽을 쓸 예정이었습니다만, 상상 이상으로 쓰고 싶은 내용이 많으므로, 해설은 별도 기사 로 합니다. 이 기사에서는 PNG, EPS에 출력하는 코드의 샘플을 올릴 예정입니다.
기본, 코멘트 아웃의 개소를 편집하는 것으로 그래프를 변경할 수 있다고 생각합니다.
당연하지만, 불명한 점은, gnuplot 자체, Graphic.Gnuplot 둘 다 조사하는 것이 포인트입니다.
 2D 궤적
import Graphics.Gnuplot.Simple
--z::[[(Double,Double)]] legend::[String]
costumPlotPaths z legend = plotPathsStyle atribute plotstyle
  where
    fontType = "Helvetica"
    tics     = Custom "tics"   ["font","\""++fontType++",16\""] -- 目盛りのフォントの変更
    xlavel   = Custom "xlabel" ["font","\""++fontType++",20\""] -- xlabelのフォントの変更
    ylavel   = Custom "ylabel" ["font","\""++fontType++",20\""] -- ylabelのフォントの変更
    keyFont  = Custom "key"    ["font","\""++fontType++",18\""] -- 凡例のフォントの変更    
    titleFont= Custom "title"  ["font","\""++fontType++",24\""] -- タイトルのフォントの変更    
    key = [(Key (Just["box lt 8 lw 1"]))]--凡例の枠囲み凡例の位置変更はこのリストに(Key (Just["right bottom"]))等を追加
    label = [(YLabel "y"),(XLabel "x")]--軸の見出し
    save = [EPS "test.eps",Custom "terminal" ["eps","enhanced","color"],Custom "bmargin" ["4"]]
    --save = [PNG "test.png"] -- PNGで出力するときは上の一文をコメントアウトしこれを使う。
    title = [Title "sin and arcsin"]--グラフのタイトル
    size = [Aspect (Ratio 0.7)]--グラフの形 縦/横
    font = [tics,xlavel,ylavel,keyFont,titleFont]
    atribute = (save++key++label++title++size++font)--fontは後述
    plotstyle = [x|i <-[0..(length z-1)],let x = (defaultStyle {lineSpec = CustomStyle [(LineTitle (legend!!i)),(LineWidth 2.0)]},z!!i)]
--例
main = do
  let x = linearScale 1000 ((-2::Double)*pi,2*pi)
  let y = fmap sin x
  let z = zip x y
  let w = zip y x
  let legend = ["sin","arcsin"]--線の名前、凡例に反映
  costumPlotPaths [z,w] legend
 
 향후 예정
서둘러 해설 기사를 갱신, 그래프의 종류를 늘립니다.
                
                    
        
    
    
    
    
    
                
                
                
                
                    
                        
                            
                            
                            Reference
                            
                            이 문제에 관하여(Haskell에서 그림 그리기 (gnuplot) -사용 예 -), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다
                                
                                https://qiita.com/tkch_pe/items/1510338cf9fb324b7ab9
                            
                            
                            
                                텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
                            
                            
                                
                                
                                 우수한 개발자 콘텐츠 발견에 전념
                                (Collection and Share based on the CC Protocol.)
                                
                                
                                우수한 개발자 콘텐츠 발견에 전념
                                (Collection and Share based on the CC Protocol.)
                            
                            
                        
                    
                
                
                
            
sudo apt-get install gnuplot
cabal install gnuplot
import Graphics.Gnuplot.Simple
import Graphics.Gnuplot.Simple
--z::[[(Double,Double)]] legend::[String]
costumPlotPaths z legend = plotPathsStyle atribute plotstyle
  where
    fontType = "Helvetica"
    tics     = Custom "tics"   ["font","\""++fontType++",16\""] -- 目盛りのフォントの変更
    xlavel   = Custom "xlabel" ["font","\""++fontType++",20\""] -- xlabelのフォントの変更
    ylavel   = Custom "ylabel" ["font","\""++fontType++",20\""] -- ylabelのフォントの変更
    keyFont  = Custom "key"    ["font","\""++fontType++",18\""] -- 凡例のフォントの変更    
    titleFont= Custom "title"  ["font","\""++fontType++",24\""] -- タイトルのフォントの変更    
    key = [(Key (Just["box lt 8 lw 1"]))]--凡例の枠囲み凡例の位置変更はこのリストに(Key (Just["right bottom"]))等を追加
    label = [(YLabel "y"),(XLabel "x")]--軸の見出し
    save = [EPS "test.eps",Custom "terminal" ["eps","enhanced","color"],Custom "bmargin" ["4"]]
    --save = [PNG "test.png"] -- PNGで出力するときは上の一文をコメントアウトしこれを使う。
    title = [Title "sin and arcsin"]--グラフのタイトル
    size = [Aspect (Ratio 0.7)]--グラフの形 縦/横
    font = [tics,xlavel,ylavel,keyFont,titleFont]
    atribute = (save++key++label++title++size++font)--fontは後述
    plotstyle = [x|i <-[0..(length z-1)],let x = (defaultStyle {lineSpec = CustomStyle [(LineTitle (legend!!i)),(LineWidth 2.0)]},z!!i)]
--例
main = do
  let x = linearScale 1000 ((-2::Double)*pi,2*pi)
  let y = fmap sin x
  let z = zip x y
  let w = zip y x
  let legend = ["sin","arcsin"]--線の名前、凡例に反映
  costumPlotPaths [z,w] legend

향후 예정
서둘러 해설 기사를 갱신, 그래프의 종류를 늘립니다.
Reference
이 문제에 관하여(Haskell에서 그림 그리기 (gnuplot) -사용 예 -), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://qiita.com/tkch_pe/items/1510338cf9fb324b7ab9텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
                                
                                
                                
                                
                                
                                우수한 개발자 콘텐츠 발견에 전념
                                (Collection and Share based on the CC Protocol.)