Heatmaps in Julia

How to make Heatmaps in Julia with Plotly.


Heatmap

using PlotlyJS

data = [1 20 30; 20 1 60; 30 60 1]
plot(heatmap(z=data))
Click to copy

Customizing the axes and labels on a heatmap

You can use the x, y and labels arguments to customize the display of a heatmap, and use Layout(xaxis_side="top") to move the x axis tick labels to the top:

using PlotlyJS

plot(
    heatmap(
        x=["Monday", "Tuesday", "Wednesday", "Thursday", "Friday"],
        y=["Morning", "Afternoon", "Evening"],
        z=[1 25 30 50 1; 20 1 60 80 30; 30 60 1 5 20]',
    ),
    Layout(xaxis_side="top")
)
Click to copy

Heatmap with Categorical Axis Labels

In this example we also show how to ignore hovertext when we have missing values in the data by setting the hoverongaps to false.

using PlotlyJS

plot(heatmap(
    z=[[1, missing, 30, 50, 1], [20, 1, 60, 80, 30], [30, 60, 1, -10, 20]],
    x=["Monday", "Tuesday", "Wednesday", "Thursday", "Friday"],
    y=["Morning", "Afternoon", "Evening"],
    hoverongaps=false
))
Click to copy

Heatmap with Unequal Block Sizes

using PlotlyJS
# Build the rectangles as a heatmap
# specify the edges of the heatmap squares
phi = (1 + sqrt(5) )/2. # golden ratio
xe = [0, 1, 1+(1/(phi^4)), 1+(1/(phi^3)), phi]
ye = [0, 1/(phi^3), 1/phi^3+1/phi^4, 1/(phi^2), 1]

z = [13 3 3 5; 13 2 1 5; 13 10 11 12; 13 8 8 8]'

trace = heatmap(
    x=sort(xe),
    y=sort(ye),
    z=z,
    type="heatmap",
    colorscale="Viridis"
)

# Add spiral line plot
function spiral(th)
    a = 1.120529
    b = 0.306349
    r = a .* exp.(-b .* th)
    return (r .* cos.(th), r .* sin.(th))
end

theta = range(-pi/13, stop=4*pi, length=1000)
(x, y) = spiral(theta)

trace1 = scatter(
    x=x[1].-x,
    y=-y[1].+y,
    line=attr(color="white", width=3)
)

axis_template = attr(range = [0,1.6], autorange = false,
             showgrid = false, zeroline = false,
             linecolor = "black", showticklabels = false,
             ticks = "" )

layout = Layout(
    margin=attr(t=200, r=200,b=200,l=200),
    xaxis=axis_template,
    yaxis=axis_template,
    showlegend=false,
    width=700,
    height=700,
    autosize=false
)

plot([trace, trace1], layout)
Click to copy

Heatmap with Datetime Axis

using PlotlyJS, Dates, Distributions

programmers = ["Alex","Nicole","Sara","Etienne","Chelsea","Jody","Marianne"]

base = Dates.now()
# Get last 180 days
dates = Dates.now() .- Dates.Day.(0:179)

z = rand(Poisson(), size(dates)[1], size(programmers)[1])

trace = heatmap(z=z, x=dates, y=programmers, colorscale="Viridis")
layout = Layout(title="Github commits per day", xaxis_nticks=36)

plot(trace, layout)
Click to copy

Reference

See https://plotly.com/julia/reference/heatmap/ for more information and chart attribute options!