Heatmap with labels per block

Is it possible to generate something like this (https://www.mathworks.com/help/matlab/ref/heatmap.html) with Plots.jl? Especially the labels inside the blocks.

I could only find this example: http://docs.juliaplots.org/latest/examples/gr/#heatmap-categorical-axes-and-aspect_ratio , but there are no labels.

That link is dead for me - can you paste the picture itself?

I don’t think there’s anything like that directly supported, but you can do

using Plots
x = ["a","b"]
y = ["c","d","e"]
z = (1:3).+(1:2)'
heatmap( x, y, z, c=:Blues )
annotate!( vec(tuple.((1:length(x))'.-0.5, (1:length(y)).-0.5, string.(z))) )

tmp
Adjusting the color for each annotation to have good contrast with the background would be more work, though.

3 Likes

VegaLite.jl version that includes the actual count calculation:

df = DataFrame(Id=rand(Int, 1000), Smoker=rand(Bool, 1000), HealthStatus=rand(["Excellent", "Fair", "Good", "Poor"], 1000))

df |>
  @vlplot(
    x="Smoker:n",
    y=:HealthStatus,
    title="Count of HealthStatus vs. Smoker",
    width=400,
    height=400,
    background=:white) + 
  @vlplot(:rect, color={"count()", legend={title=nothing}, scale={scheme=:blues}}) + 
  @vlplot(:text, text="count()")

This adds two layers to the base plot, one for the rects and one for the text.

EDIT: Oh, and here is how it looks:
figure

2 Likes