好的,我被Haskell Chart库感到难过。我已经想出了这种方法来制作Kronos Haskell中Vector
值的对数日志折线图:
import Data.Vector (Vector, (!))
import qualified Data.Vector as V
import Graphics.Rendering.Chart.Easy hiding (Vector)
logLogChart name points = toRenderable $ execEC $ plot chart
where chart = line name [V.toList $ V.imap makePoint points]
makePoint x y = (LogValue (fromIntegral (x+1)), LogValue y)
这肯定会渲染一个合理的对数 - 对数线图,根据数据自动选择x和y轴的范围。一个例子(在Kronos Haskell中呈现):
问题在于我有一个专门的应用程序,我需要这两件事:
Vector
,因此可以简化为y轴的范围由x轴的范围确定。)我试着查看了图书馆的文档,但它让我完全难过。有什么指针吗?
答案 0 :(得分:1)
这样做但可以大大改进(尤其是删除unsafePerformIO)。
{-# OPTIONS_GHC -Wall #-}
{-# OPTIONS_GHC -fno-warn-name-shadowing #-}
{-# OPTIONS_GHC -fno-warn-type-defaults #-}
{-# OPTIONS_GHC -fno-warn-unused-do-bind #-}
{-# OPTIONS_GHC -fno-warn-missing-methods #-}
{-# OPTIONS_GHC -fno-warn-orphans #-}
import Graphics.Rendering.Chart hiding ( translate )
import Graphics.Rendering.Chart.Backend.Diagrams
import Diagrams.Backend.Cairo.CmdLine
import Diagrams.Prelude hiding ( render, Renderable )
import Data.Default.Class
import Diagrams.Backend.CmdLine
import System.IO.Unsafe
pointVals :: [(Double, Double)]
pointVals = map (\(x,y) -> (log x, log y)) [(1,10), (10, 100), (100, 1000)]
dataPts :: PlotPoints Double Double
dataPts = plot_points_style .~ filledCircles 2 (opaque red)
$ plot_points_values .~ pointVals
$ plot_points_title .~ "Data points"
$ def
layout :: Layout Double Double
layout = layout_title .~ "Log vs Log"
$ layout_y_axis . laxis_generate .~ scaledAxis def (0,10)
$ layout_x_axis . laxis_generate .~ scaledAxis def (0,10)
$ layout_plots .~ [toPlot dataPts]
$ def
myChart :: Renderable ()
myChart = toRenderable layout
denv :: DEnv
denv = unsafePerformIO $ defaultEnv vectorAlignmentFns 500 500
displayHeader :: FilePath -> Diagram B R2 -> IO ()
displayHeader fn =
mainRender ( DiagramOpts (Just 900) (Just 700) fn
, DiagramLoopOpts False Nothing 0
)
myDiagram :: Diagram Cairo R2
myDiagram = fst $ runBackend denv (render myChart (500, 500))
main :: IO ()
main = displayHeader "LogChart.png" myDiagram