# How to draw heatmap with DataFrames

**URL:** <https://discourse.julialang.org/t/how-to-draw-heatmap-with-dataframes/15321>\
**Category:** Visualization\
**Created:** [September 22, 2018, 12:11am UTC](https://discourse.julialang.org/t/how-to-draw-heatmap-with-dataframes/15321 "2018-09-22T00:11:13Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![tk3369](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tk3369/32/2824_2.png) [@tk3369](https://discourse.julialang.org/u/tk3369)\
**Post date:** [September 22, 2018, 12:11am UTC](https://discourse.julialang.org/t/how-to-draw-heatmap-with-dataframes/15321/1 "2018-09-22T00:11:13Z")

</div>

Let’s say I have a data frame like this, how do I make a heatmap? I’m already using StatPlots so that would be the preferred way. I tried to look up documentation but couldn’t find it…

```julia
data = DataFrame(
    src = ["London", "London", "New York", "Hong Kong"],
    dst = ["Beijing", "New York", "Boston", "Boston"],
    val = [1000.00, 2000.00, 3000.00, 4000.00]
)

```

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [September 22, 2018, 3:05am UTC](https://discourse.julialang.org/t/how-to-draw-heatmap-with-dataframes/15321/2 "2018-09-22T03:05:49Z")

</div>

There is not enough data to draw a heat map. What do you want the plot to look like?

---

<div class="post-metadata">

**Author:** ![tk3369](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tk3369/32/2824_2.png) [@tk3369](https://discourse.julialang.org/u/tk3369)\
**Post date:** [September 22, 2018, 4:58am UTC](https://discourse.julialang.org/t/how-to-draw-heatmap-with-dataframes/15321/3 "2018-09-22T04:58:19Z")

</div>

The data above is just a sample. I worked that out manually but was hoping for an easier/existing solution. Here’s what I did:

```julia
# Return data required for heatmap
function make_heatmap_data(data, x, y, v)
    xs = unique(data[x])
    ys = unique(data[y])
    n = length(xs)
    m = length(ys)
    A = zeros((n, m))
    D1 = Dict(x => i for (i,x) in enumerate(xs))
    D2 = Dict(x => i for (i,x) in enumerate(ys))
    for i in 1:size(data, 1)
        xi = data[i, x]
        yi = data[i, y]
        vi = data[i, v]
        A[D1[xi], D2[yi]] = vi
    end
    (xs, ys, A)
end

let (x, y, A) = make_heatmap_data(data, :src, :dst, :val)
    heatmap(x, y, A, seriescolor = :blues, size = (400,300))
end

```

![heatmap_example](https://global.discourse-cdn.com/julialang/original/3X/b/6/b6fcf74b9e62bbffb8821ea1c530abebbf429f61.png)

---

<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:** [November 12, 2018, 7:20pm UTC](https://discourse.julialang.org/t/how-to-draw-heatmap-with-dataframes/15321/4 "2018-11-12T19:20:02Z")

</div>

Here is how you can do it with [VegaLite.jl](https://github.com/fredo-dedup/VegaLite.jl):

```julia
data |> @vlplot(:rect, x=:src, y=:dst, color=:val)

```

That gets me:  
 ![visualization%20(2)](https://global.discourse-cdn.com/julialang/original/3X/d/3/d3b324bd3fbb62d5918ba845e3c9ef4ee411790d.png)

Probably worth playing around a bit more with the size of the plot etc., but I also assume that would just look better with more data.
