Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Heatmap Example just isn't building #48

Open
MilesLitteral opened this issue May 11, 2022 · 5 comments
Open

Heatmap Example just isn't building #48

MilesLitteral opened this issue May 11, 2022 · 5 comments

Comments

@MilesLitteral
Copy link

I made a test to use the heatmap example in:

import Plots
import Linear.V2 
import Control.Lens

main :: IO ()
main = do
    let xs = [[1,2,3],[4,5,6]]
    heatMap xs $ heatMapSize .= V2 10 10

But it fails and is unbuildable with the following errors:

    • No instance for (base-4.15.1.0:Data.Typeable.Internal.Typeable
                         a0)
        arising from a use of ‘heatMap’
    • In the first argument of ‘($)’, namely ‘heatMap xs’
      In a stmt of a 'do' block: heatMap xs $ (heatMapSize .= V2 10 10)
      In the expression:
        do let xs = ...
           heatMap xs $ (heatMapSize .= V2 10 10)
   |
19 |     heatMap xs $ (heatMapSize .= V2 10 10)


    • Ambiguous type variable ‘a0’ arising from the literal ‘10’
      prevents the constraint ‘(Num a0)’ from being solved.
      Probable fix: use a type annotation to specify what ‘a0’ should be.
      These potential instances exist:
        instance Num a => Num (Identity a)
          -- Defined in ‘Data.Functor.Identity’
        instance Num a => Num (V2 a) -- Defined in ‘Linear.V2’
        instance Num Integer -- Defined in ‘GHC.Num’
        ...plus four others
        ...plus 10 instances involving out-of-scope types
        (use -fprint-potential-instances to see them all)
    • In the first argument of ‘V2’, namely ‘10’
      In the second argument of ‘(.=)’, namely ‘V2 10 10’
      In the second argument of ‘($)’, namely ‘(heatMapSize .= V2 10 10)’
   |
19 |     heatMap xs $ (heatMapSize .= V2 10 10)
   |                                     ^^

I took out heatMapAxis because it keeps complaining that it's constraint (Axis B V2 Double)
is unsolvable in terms of B and I don't understand how to remedy it not finding a backend.
There doesn't seem to be a lens for it, or any reference to the backend being used but
considering it's capitalized there has to be something?

Any advice is appreciated, Thanks

@MilesLitteral
Copy link
Author

I wanted to add on that writing the heatmap example like this also doesn't work

import Plots
import Linear.V2 
import Control.Lens

heatMapAxis :: Axis B V2 Double
heatMapAxis = r2Axis &~ do
  display colourBar
  axisExtend .= noExtend

  let xs = [[1,2,3],[4,5,6]]
  heatMap xs $ heatMapSize .= V2 10 10

instead it throws this error

    Not in scope: type constructor or class ‘B’
  |
5 | heatMapAxis :: Axis B V2 Double
  |                     ^

@cchalmers
Copy link
Owner

You need to import a backend. Each backend exports a B type alias to make it easier to switch between backends. You can see some examples that hopefully still compile in https://github.com/cchalmers/plots/tree/master/examples. I don't have a heatMap example there but they should work the same. Those examples import Diagrams.Backend.Rasterific.CmdLine for the backend.

@MilesLitteral
Copy link
Author

Thanks! This updated version of the example code builds

{-# LANGUAGE FlexibleContexts #-}
module Main where 

import Plots
import Linear.V2 
import Control.Lens
import Control.Monad.IO.Class
import Diagrams.Backend.Rasterific.CmdLine

heatMapAxis :: Axis B V2 Double
heatMapAxis = r2Axis &~ do
  display colourBar
  axisExtend .= noExtend

  let xs = [[1,2,3],[4,5,6]]
  heatMap xs $ heatMapSize .= V2 10 10

main :: IO ()
main =  r2AxisMain $ heatMapAxis

when running the example GHC points out as follows:
No output file given. Specify output file with -o

Is this the only way to force the output of the heatmap? Is there a way to SaveImage (like JuicyPixels)

Thank you very much your answered fixed the original problem and addressed my original question :)

@MilesLitteral
Copy link
Author

MilesLitteral commented May 11, 2022

as a poor man's work around I discovered you can do this:

{-# LANGUAGE FlexibleContexts #-}

module Main where 

import Plots
import Linear.V2 
import Control.Lens
import Control.Monad.IO.Class

import Codec.Picture
import Diagrams.Backend.Rasterific.CmdLine

heatMapIndexedAxis :: Axis B V2 Double
heatMapIndexedAxis = r2Axis &~ do
  display colourBar
  axisExtend .= noExtend

  let f (V2 x y) = fromIntegral x + fromIntegral y
  heatMapIndexed (V2 3 3) f $ heatMapSize .= V2 10 10
  
main :: IO ()
main =  r2AxisMain $ heatMapIndexedAxis

Then run this in cmd:
stack exec --<module> -o out.png

I'm sure a more standard solution will come up in time; thanks again for your help!

@cchalmers
Copy link
Owner

cchalmers commented May 11, 2022

Happy to help. Yes, rendering directly is possible. You can call renderAxis and then run renderRasterific on the resulting Diagram.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants