# How to use \`heatmap\` in GR.jl

**URL:** <https://discourse.julialang.org/t/how-to-use-heatmap-in-gr-jl/49507>\
**Category:** General Usage\
**Tags:** question, plotting, gr\
**Created:** [November 3, 2020, 7:47am UTC](https://discourse.julialang.org/t/how-to-use-heatmap-in-gr-jl/49507 "2020-11-03T07:47:05Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![kapple](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kapple/32/218915_2.png) [@kapple](https://discourse.julialang.org/u/kapple)\
**Post date:** [November 3, 2020, 7:47am UTC](https://discourse.julialang.org/t/how-to-use-heatmap-in-gr-jl/49507/1 "2020-11-03T07:47:05Z")

</div>

Why does this not work?

```julia
julia> using GR

julia> heatmap([1 2], [3 4], [5 6; 7 8])
ERROR: AssertionError: length(x) == dimx + 1 && length(y) == dimy + 1
Stacktrace:
 [1] nonuniformcellarray(::Array{Int64,2}, ::Array{Int64,2}, ::Int64, ::Int64, ::Array{Int64,2}) at C:\Users\aaron\.julia\packages\GR\8mv9N\src\GR.jl:754
 [2] plot_data(::Bool) at C:\Users\aaron\.julia\packages\GR\8mv9N\src\jlgr.jl:1306
 [3] plot_data at C:\Users\aaron\.julia\packages\GR\8mv9N\src\jlgr.jl:1074 [inlined]
 [4] heatmap(::Array{Int64,2}, ::Array{Int64,2}, ::Array{Int64,2}; kv::Base.Iterators.Pairs{Union{},Union{},Tuple{},NamedTuple{(),Tuple{}}}) at C:\Users\aaron\.julia\packages\GR\8mv9N\src\jlgr.jl:2052
 [5] heatmap at C:\Users\aaron\.julia\packages\GR\8mv9N\src\jlgr.jl:2047 [inlined]
 [6] #heatmap#17 at C:\Users\aaron\.julia\packages\GR\8mv9N\src\GR.jl:3419 [inlined]
 [7] heatmap(::Array{Int64,2}, ::Array{Int64,2}, ::Array{Int64,2}) at C:\Users\aaron\.julia\packages\GR\8mv9N\src\GR.jl:3419
 [8] top-level scope at REPL[2]:1

```

I’m pretty sure people are going to recommend using `Plots.jl` but I’m trying to select a plotting package to use in my package development, and Plots takes waaaaaaaaaaaaay too long to precompile and ttfp just does my head in.

Should I open an issue on GitHub?

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [November 3, 2020, 8:00am UTC](https://discourse.julialang.org/t/how-to-use-heatmap-in-gr-jl/49507/2 "2020-11-03T08:00:42Z")

</div>

No idea why the GR code wouldn’t work, but on the Plots issue: are you referring to Plots being too slow while you develop your package (i.e. in an iterative workflow where you just use Plots to visualise things every now and then) or Plots being too slow to precompile to integrate into your package as a dependency?

If it’s the former, are you using Revise for development? That should largely obviate the need to re-start your Julia session, so you only pay compilation cost once. If for some reasons you still have to re-start frequently you might want to consider having Plots in a custom sysimage using PackageCompiler.jl

If the latter, have you looked at Recipes? They’re made so you don’t have to depend on Plots in your package, but just on RecipesBase, and users will then `using Plots` separately if they want to plot.

Finally just so I give at least **some** info related to your actual question, have you tried [GRUtils.jl](https://github.com/heliosdrm/GRUtils.jl)? I don’t really know anything about it, but it seems that the GR developers recommend using this if one wants to work with GR directly (without the Plots wrapper).

---

<div class="post-metadata">

**Author:** ![kapple](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kapple/32/218915_2.png) [@kapple](https://discourse.julialang.org/u/kapple)\
**Post date:** [November 3, 2020, 8:07am UTC](https://discourse.julialang.org/t/how-to-use-heatmap-in-gr-jl/49507/3 "2020-11-03T08:07:39Z")

</div>

> [@nilshg](#):
>
> If it’s the former, are you using Revise for development?

Yes, I use Revise. My current development involves redefining a lot of structs, which requires restarting Julia.

> [@nilshg](#):
>
> you might want to consider having Plots in a custom sysimage using PackageCompiler.jl

I’m not sure what that means, but I’ll look into that, thanks!

> [@nilshg](#):
>
> If the latter, have you looked at Recipes? They’re made so you don’t have to depend on Plots in your package, but just on RecipesBase, and users will then `using Plots` separately if they want to plot.

For the output data I’m working on, there are a lot of features involved to look at, so I’ve produced a support niche plotting function that I can just pass the output data/struct instantiations to which will do all the steps of plotting for me. Super nice and convenient – in theory, if I can get along well with the plotting package.

> [@nilshg](#):
>
> have you tried [GRUtils.jl](https://github.com/heliosdrm/GRUtils.jl)?

Yep! I’m actually using it, but it’s just a little finicky and has a few bugs (which is not a surprise, it’s still in development), so I decided to check out other options, but I may stick with GRUtils for now since GR can’t even (supposedly) plot a heatmap.

I love the API of Plots.jl, but bloody heck it takes so long to load.

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [November 3, 2020, 10:25am UTC](https://discourse.julialang.org/t/how-to-use-heatmap-in-gr-jl/49507/4 "2020-11-03T10:25:06Z")

</div>

Again slightly off-topic, but for your development workflow consider:

- Renaming rather than redefining structs - so work with `MyStruct_1`, when that doesn’t pan out to a find/replace `MyStruct_1 => MyStruct_2` etc to avoid having to restart
- Working with `NamedTuple`s rather than structs

Also I think it should be possible to have quite complex plots with different features using `RecipeBase`

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [November 3, 2020, 10:57am UTC](https://discourse.julialang.org/t/how-to-use-heatmap-in-gr-jl/49507/5 "2020-11-03T10:57:28Z")

</div>

> [@kapple](#):
>
> `julia> heatmap([1 2], [3 4], [5 6; 7 8])`

`[1 2]` and `[3 4]` are matrices with one row and two columns, while heatmap is expecting a vector (one column and two rows):

```julia-auto
julia> heatmap([1, 2], [3, 4], [5 6; 7 8])

```

 ![plot](https://global.discourse-cdn.com/julialang/original/3X/3/1/31efb360510a1888eb27e3367dd12c26651045c2.png)

```julia-auto
julia> [3 4]
1×2 Array{Int64,2}:
 3 4

julia> [3,4]
2-element Array{Int64,1}:
 3
 4

```

---

<div class="post-metadata">

**Author:** ![kapple](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kapple/32/218915_2.png) [@kapple](https://discourse.julialang.org/u/kapple)\
**Post date:** [November 3, 2020, 11:14am UTC](https://discourse.julialang.org/t/how-to-use-heatmap-in-gr-jl/49507/6 "2020-11-03T11:14:35Z")

</div>

GAH why didn’t I think of that.

Good catch thanks.

---

<div class="post-metadata">

**Author:** ![kapple](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kapple/32/218915_2.png) [@kapple](https://discourse.julialang.org/u/kapple)\
**Post date:** [November 3, 2020, 11:15am UTC](https://discourse.julialang.org/t/how-to-use-heatmap-in-gr-jl/49507/7 "2020-11-03T11:15:56Z")

</div>

I’ve only been learning Julia recently, and `NamedTuple`s is new to me. I’ll check it out, thank you!

Does Julia optimize with `NamedTuple`s well?

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [November 3, 2020, 11:22am UTC](https://discourse.julialang.org/t/how-to-use-heatmap-in-gr-jl/49507/8 "2020-11-03T11:22:50Z")

</div>

> [@nilshg](#):
>
> Renaming rather than redefining structs - so work with `MyStruct_1` , when that doesn’t pan out to a find/replace `MyStruct_1 => MyStruct_2` etc to avoid having to restart

Couldn’t that be a strategy for `Revise` to deal with the struct changes? It could add a marker like `_2` while loading the code, and perhaps warn the user of that, so that possible error messages are clear.

---

<div class="post-metadata">

**Author:** ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)\
**Post date:** [November 3, 2020, 11:38am UTC](https://discourse.julialang.org/t/how-to-use-heatmap-in-gr-jl/49507/9 "2020-11-03T11:38:53Z")

</div>

> [@lmiq](#):
>
> Couldn’t that be a strategy for `Revise` to deal with the struct changes?

> <https://github.com/timholy/Revise.jl/issues/18>
>
> Since \`struct\`s cannot be redefined, attempting to do so currently gives a \`fail…ure to evaluate changes\` warning.
> 
> I wonder if this can be worked around by triggering a reload of the entire module in this case.

---

<div class="post-metadata">

**Author:** ![kapple](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kapple/32/218915_2.png) [@kapple](https://discourse.julialang.org/u/kapple)\
**Post date:** [November 4, 2020, 10:03am UTC](https://discourse.julialang.org/t/how-to-use-heatmap-in-gr-jl/49507/10 "2020-11-04T10:03:10Z")

</div>

Back to the original topic, even your MWE response isn’t working for me.

```julia
julia> using GR

julia> heatmap([1, 2], [3, 4], [5 6; 7 8])
ERROR: AssertionError: length(x) == dimx + 1 && length(y) == dimy + 1
Stacktrace:
 [1] nonuniformcellarray(::Array{Int64,1}, ::Array{Int64,1}, ::Int64, ::Int64, ::Array{Int64,2}) at C:\Users\aaron\.julia\packages\GR\8mv9N\src\GR.jl:754
 [2] plot_data(::Bool) at C:\Users\aaron\.julia\packages\GR\8mv9N\src\jlgr.jl:1306
 [3] plot_data at C:\Users\aaron\.julia\packages\GR\8mv9N\src\jlgr.jl:1074 [inlined]
 [4] heatmap(::Array{Int64,1}, ::Array{Int64,1}, ::Array{Int64,2}; kv::Base.Iterators.Pairs{Union{},Union{},Tuple{},NamedTuple{(),Tuple{}}}) at C:\Users\aaron\.julia\packages\GR\8mv9N\src\jlgr.jl:2052
 [5] heatmap at C:\Users\aaron\.julia\packages\GR\8mv9N\src\jlgr.jl:2047 [inlined]
 [6] #heatmap#17 at C:\Users\aaron\.julia\packages\GR\8mv9N\src\GR.jl:3419 [inlined]
 [7] heatmap(::Array{Int64,1}, ::Array{Int64,1}, ::Array{Int64,2}) at C:\Users\aaron\.julia\packages\GR\8mv9N\src\GR.jl:3419
 [8] top-level scope at REPL[2]:1

```

It’s checking that the length of the input range vectors are one element longer than their dimensions?

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [November 4, 2020, 10:39am UTC](https://discourse.julialang.org/t/how-to-use-heatmap-in-gr-jl/49507/11 "2020-11-04T10:39:22Z")

</div>

> [@kapple](#):
>
> `heatmap([1, 2], [3, 4], [5 6; 7 8])`

Sorry, I tested it here with Plots with the gr backend:

```julia-auto
julia> using Plots

julia> gr() # default anyway
Plots.GRBackend()

julia> heatmap([1, 2], [3, 4], [5 6; 7 8])
qt5ct: using qt5ct plugin

```

---

<div class="post-metadata">

**Author:** ![kapple](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kapple/32/218915_2.png) [@kapple](https://discourse.julialang.org/u/kapple)\
**Post date:** [November 4, 2020, 10:42am UTC](https://discourse.julialang.org/t/how-to-use-heatmap-in-gr-jl/49507/12 "2020-11-04T10:42:14Z")

</div>

Cool, but I’m wondering if I can use GR.jl directly. Plots.jl takes too long to load, and if I can access all the functionality I want with just one of the backends, then I’ll do just that.

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [November 4, 2020, 10:48am UTC](https://discourse.julialang.org/t/how-to-use-heatmap-in-gr-jl/49507/13 "2020-11-04T10:48:13Z")

</div>

In the GR docs they use `LinRange` to generate the `x` and `y`:

[https://gr-framework.org/julia-jlgr.html#heatmap-849ebfcad83c4c0251a8873748f01036](https://gr-framework.org/julia-jlgr.html#heatmap-849ebfcad83c4c0251a8873748f01036)

---

<div class="post-metadata">

**Author:** ![kapple](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kapple/32/218915_2.png) [@kapple](https://discourse.julialang.org/u/kapple)\
**Post date:** [November 4, 2020, 11:08am UTC](https://discourse.julialang.org/t/how-to-use-heatmap-in-gr-jl/49507/14 "2020-11-04T11:08:03Z")

</div>

So apparently this works

```julia
julia> heatmap(z)

```

but this doesn’t work

```julia
julia> heatmap(x, y, z)

```

Why it doesn’t work is beyond me. I don’t understand the difference in functionality between the two in terms of, why wouldn’t the second one work? Is it still in development?

They even have a [little function declaration box](https://gr-framework.org/julia-jlgr.html#heatmap-332841889cbc5ae768a1c4ff7cf23e08) saying it should work?

![image](https://global.discourse-cdn.com/julialang/original/3X/7/0/7000b2bca045d8c023ed30085c167dacba4abbc0.png)

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [November 4, 2020, 11:18am UTC](https://discourse.julialang.org/t/how-to-use-heatmap-in-gr-jl/49507/15 "2020-11-04T11:18:21Z")

</div>

Have you tried the example as it is posted in the manual? It does not work?

---

<div class="post-metadata">

**Author:** ![kapple](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kapple/32/218915_2.png) [@kapple](https://discourse.julialang.org/u/kapple)\
**Post date:** [November 4, 2020, 11:21am UTC](https://discourse.julialang.org/t/how-to-use-heatmap-in-gr-jl/49507/16 "2020-11-04T11:21:11Z")

</div>

Yes I did try the example and it works.

However, the example given does not take in `x` and `y` as the first two inputs. It only takes in `z`.

Passing `x`, `y`, _and_ `z` doesn’t work.

The doc mentions something about changing the limits of the heatmap plot, so I might check that out instead.

I might read as much of the doc as is relevant, and if I don’t find an explanation for why `heatmap(x, y, z)` doesn’t work, then I’ll open an issue on GitHub.

---

<div class="post-metadata">

**Author:** ![briochemc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/briochemc/32/4209_2.png) [@briochemc](https://discourse.julialang.org/u/briochemc)\
**Post date:** [November 4, 2020, 12:19pm UTC](https://discourse.julialang.org/t/how-to-use-heatmap-in-gr-jl/49507/17 "2020-11-04T12:19:50Z")

</div>

It looks like maybe GR expects `x` and `y` to be the edges of the heatmap grid. Could you try

```julia
heatmap([0.5, 1.5, 2.5], [2.5, 3.5, 4.5], [5 6; 7 8])

```

instead?

---

<div class="post-metadata">

**Author:** ![kapple](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kapple/32/218915_2.png) [@kapple](https://discourse.julialang.org/u/kapple)\
**Post date:** [November 4, 2020, 12:44pm UTC](https://discourse.julialang.org/t/how-to-use-heatmap-in-gr-jl/49507/18 "2020-11-04T12:44:09Z")

</div>

Dang it, I feel stupid.

Yep, it works. Thanks!
