# Heatmap with labels per block

**URL:** <https://discourse.julialang.org/t/heatmap-with-labels-per-block/29152>\
**Category:** Visualization\
**Tags:** question, plotting\
**Created:** [September 25, 2019, 9:44am UTC](https://discourse.julialang.org/t/heatmap-with-labels-per-block/29152 "2019-09-25T09:44:15Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![feanor12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/feanor12/32/8212_2.png) [@feanor12](https://discourse.julialang.org/u/feanor12)\
**Post date:** [September 25, 2019, 9:44am UTC](https://discourse.julialang.org/t/heatmap-with-labels-per-block/29152/1 "2019-09-25T09:44:15Z")

</div>

Is it possible to generate something like this ([Create heatmap chart - MATLAB heatmap](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](http://docs.juliaplots.org/latest/examples/gr/#heatmap-categorical-axes-and-aspect_ratio) , but there are no labels.

---

<div class="post-metadata">

**Author:** ![mkborregaard](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkborregaard/32/556_2.png) [@mkborregaard](https://discourse.julialang.org/u/mkborregaard)\
**Post date:** [September 25, 2019, 2:54pm UTC](https://discourse.julialang.org/t/heatmap-with-labels-per-block/29152/2 "2019-09-25T14:54:33Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![feanor12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/feanor12/32/8212_2.png) [@feanor12](https://discourse.julialang.org/u/feanor12)\
**Post date:** [September 25, 2019, 3:05pm UTC](https://discourse.julialang.org/t/heatmap-with-labels-per-block/29152/3 "2019-09-25T15:05:09Z")

</div>

![](https://global.discourse-cdn.com/julialang/original/3X/6/7/674e511631c59d4fd0719ffce480fd13295d29a9.png)

---

<div class="post-metadata">

**Author:** ![yha](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yha/32/3502_2.png) [@yha](https://discourse.julialang.org/u/yha)\
**Post date:** [September 25, 2019, 5:02pm UTC](https://discourse.julialang.org/t/heatmap-with-labels-per-block/29152/4 "2019-09-25T17:02:31Z")

</div>

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

```julia
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](https://global.discourse-cdn.com/julialang/original/3X/c/a/ca695bdc81f21f36d11b34a0056b244edbf6bbb0.png)  
Adjusting the color for each annotation to have good contrast with the background would be more work, though.

---

<div class="post-metadata">

**Author:** ![davidanthoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/davidanthoff/32/223493_2.png) [@davidanthoff](https://discourse.julialang.org/u/davidanthoff)\
**Post date:** [September 25, 2019, 6:33pm UTC](https://discourse.julialang.org/t/heatmap-with-labels-per-block/29152/5 "2019-09-25T18:33:58Z")

</div>

[VegaLite.jl](https://github.com/queryverse/VegaLite.jl) version that includes the actual count calculation:

```julia
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](https://global.discourse-cdn.com/julialang/original/3X/d/2/d26a5627c21292134cead6f15bc00810f3c54e14.png)
