# Discrete colorbar with PlotlyJS

**URL:** https://discourse.julialang.org/t/discrete-colorbar-with-plotlyjs/80773
**Category:** Visualization
**Tags:** plotting, plotlyjs
**Created:** [May 9, 2022, 7:01pm UTC](https://discourse.julialang.org/t/discrete-colorbar-with-plotlyjs/80773 "2022-05-09T19:01:46Z")
**Posts on this page:** 1
**Showing post:** 2

<div class="post-metadata">

### Author: ![jd-foster](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jd-foster/32/35824_2.png) [@jd-foster](https://discourse.julialang.org/u/jd-foster)
#### Post date: [May 11, 2022, 3:47am UTC](https://discourse.julialang.org/t/discrete-colorbar-with-plotlyjs/80773/2 "2022-05-11T03:47:38Z")

</div>

Here is an example:

 ![plotly-discrete-heatmap](https://global.discourse-cdn.com/julialang/original/3X/0/e/0e43b76df4b1e0c8aadca979e625cd1bbb974b26.jpeg)

The code to produce this is

```julia

using PlotlyJS

## Set-up:
# Define the categories and corresponding border levels between them:
categories = ["Undetectable", "Low", "Medium", "High", "Extreme", "Wat"]
levels = [0, 8, 16, 24, 32, 40, 48]
label_levels = levels .+ 4 # note the shift to centre the labels

# Some random data:
data = [
    29 16 15 7 14
    10 4 36 14 2
    25 23 0 29 41
]
# data = rand(minimum(levels):maximum(levels), 3, 5)

# A discrete colour scheme: see https://juliagraphics.github.io/Colors.jl/dev/namedcolors/
colour_pairs = [
    "crimson" => (220, 20, 60),
    "orangered" => (255, 69, 0),
    "goldenrod" => (218, 165, 32),
    "aquamarine3" => (102, 205, 170),
    "royalblue" => ( 65, 105, 225),
    "darkviolet" => (148, 0, 211),
    "black" => ( 0, 0, 0),
]
# Make it into a suitable list:
colour_list = map(x -> "rgb$(x[2])", colour_pairs)

# The tricky part - creating ordered ranges of colours:
norm_levels = (levels .- minimum(levels))/maximum(levels)
colour_marks = [[norm_levels[1], colour_list[1]]]
for i in 2:length(levels)
    push!(colour_marks, [norm_levels[i], colour_list[i-1]])
    push!(colour_marks, [norm_levels[i], colour_list[i]])
end

## Plot:
# Adapting http://juliaplots.org/PlotlyJS.jl/stable/examples/heatmaps/
# See https://plotly.com/julia/reference/heatmap/ for reference to keyword arguments
function heatmap2()
    trace = heatmap(
        x=["Monday", "Tuesday", "Wednesday", "Thursday", "Friday"],
        y=["Morning", "Afternoon", "Evening"],
        z=data,
        autocolorscale=false,
        colorbar=attr(tickmode="array",
                      tickvals=label_levels,
                      ticktext=categories),
        colorscale=colour_marks,
        zmin=minimum(levels),
        zmax=maximum(levels)
    )
    plot(trace)
end
p = heatmap2()

```

See also this post:

> [@Set custom colorbar tick-labels](https://discourse.julialang.org/t/set-custom-colorbar-tick-labels/69806/5):
>
> Everything is possible, with some imagination: play_button using Plots; gr()

---

_[View the full topic](https://discourse.julialang.org/t/discrete-colorbar-with-plotlyjs/80773)._
