JuliaPlots / PlotlyJS.jl

Julia library for plotting with plotly.js
Other
424 stars 78 forks source link

Manual discrete colormap #359

Closed jfb-h closed 3 years ago

jfb-h commented 3 years ago

Is it possible to use a manual discrete colormap as described in the manual for the python version?

E.g. like so, which errors:

df = DataFrame(x=rand(100), y=rand(100), g=rand(["a", "b", "c"], 100))

cmap = Dict(
    "a" => "red", 
    "b" => "blue",
    "c" => "green"
)

sc = scatter(df, x=:x, y=:y, group=:g, mode="markers", color_discrete_map=cmap)

I couldn't find anything in the documentation and tried a bunch of different things but nothing did the trick.

empet commented 3 years ago

The colorscale should look like this:

cmap = [[0.0, "rgb(255, 0, 0)"],
        [0.5, "rgb(0, 255, 0)"],
        [1.0,  "rgb(0, 0, 255)"]]

i.e. it is defined as an 3-element Array{Array{Any,1},1}.

You can give the color codes in hex format, too:

plasma = [[0.0, "#0c0786"],
          [0.1, "#40039c"],
          [0.2, "#6a00a7"],
          [0.3, "#8f0da3"],
          [0.4, "#b02a8f"],
          [0.5, "#cb4777"],
          [0.6, "#e06461"],
          [0.7, "#f2844b"],
          [0.8, "#fca635"],
          [0.9, "#fcce25"],
          [1.0, "#eff821"]] 

The latter example is the default colorscale in the Python version.

jfb-h commented 3 years ago

Thanks, good to know it should be an array not a dict! However, this does not seem to help with the discrete color mapping problem. Do you know if there's a keyword argument for that also in Julia?

empet commented 3 years ago

Could you explain what do you mean by discrete colorscale? Is this discrete colorscale https://community.plotly.com/t/colors-for-discrete-ranges-in-heatmaps/7780 what you are looking for?

jfb-h commented 3 years ago

The python manual on discrete color has a section (pretty far down) which shows how to directly assign colors to levels of a grouping variable. Here's the code snippet:

import plotly.express as px
df = px.data.gapminder().query("year == 2007")
fig = px.bar(df, y="continent", x="pop", color="continent", orientation="h", hover_name="country",
             color_discrete_map={
                "Europe": "red",
                "Asia": "green",
                "Americas": "blue",
                "Oceania": "goldenrod",
                "Africa": "magenta"},
             title="Explicit color mapping")

fig.show()

I would like to reproduce that in Julia, i.e. I'm looking for an equivalent to the color_discrete_map keyword.

empet commented 3 years ago

Plotly express is a wrapper for plotly.py that exposes a simple syntax for complex charts. Behind the scenes it performs the same computations like in the forum answers above. There is no Julia library that can imitate plotly express functions.

jfb-h commented 3 years ago

I see, thanks. I thought as PlotlyJS.jl exposes some similar features (e.g. convenience plotting with DataFrames) this might also be available.