scala-plotly-client icon indicating copy to clipboard operation
scala-plotly-client copied to clipboard

Custom colorscales

Open pbugnion opened this issue 9 years ago • 2 comments

Plotly supports sending custom colorscales as well as the predefined ones. It might be nice to try and support that.

For instance, the following request body to the Plotly API seems to do what we expect (POST https://api.plot.ly/v2/plots ; you need to set the headers Plotly-Client-Platform to something (eg. scala), Content-Type to application/json and use basic auth with your username and password):

{
   "figure":{
      "data":[
         {
            "zsrc":"pbugnion:452:0049bf,2b5279,47daf1,964c04,6841d3,961eab",
            "type":"heatmap",
            "colorscale":[
               [
                  7,
                  "rgb(165,0,38)"
               ],
               [
                  7.1111111111111111,
                  "rgb(215,48,39)"
               ],
               [
                  7.2222222222222222,
                  "rgb(244,109,67)"
               ],
               [
                  7.3333333333333333,
                  "rgb(253,174,97)"
               ],
               [
                  8.4444444444444444,
                  "rgb(254,224,144)"
               ],
               [
                  8.5555555555555556,
                  "rgb(224,243,248)"
               ],
               [
                  8.6666666666666666,
                  "rgb(171,217,233)"
               ],
               [
                  8.7777777777777778,
                  "rgb(116,173,209)"
               ],
               [
                  8.8888888888888888,
                  "rgb(69,117,180)"
               ],
               [
                  9,
                  "rgb(49,54,149)"
               ]
            ]
         }
      ]},
   "world_readable":true
}

The colorscale field seems to be an array of 2-arrays containing a z-value and a color.

I think the following interface would work:

// current interface in, eg. contour plots
val p = ThreeDPlot().withSurface(zs, SurfaceOptions().colorscale("Viridis"))

// extensions
val customColorscale = Coloscale.fromIterable(
 List(7 -> Color.rgb(255, 0, 0), 7.5 -> Color.rgb(128, 128, 0), 8.0 -> Color.rgb(0, 255, 0))
)
val p = ThreeDPlot().withSurface(zs, SurfaceOptions().colorscale(customColorscale))

val customColorscale = Coloscale.fromFunction { z => 
  if (z < 10.0) { Color.rgb(255, 0, 0) } else { Color.rgb(0, 255, 0) }
}
val p = ThreeDPlot().withSurface(zs, SurfaceOptions().colorscale(customColorscale))

pbugnion avatar Jun 19 '16 13:06 pbugnion

@srstevenson Mostly raising this so we are aware of it. At the moment, I think it's fine to just stick to a string value for the colorscale, since the more complete implementation doesn't alter the interface.

pbugnion avatar Jun 19 '16 13:06 pbugnion

This describes setting custom colorscales in Python: https://plot.ly/python/heatmap-and-contour-colorscales/

pbugnion avatar Jun 19 '16 13:06 pbugnion