# How to plot matrix as gradient grid?

**URL:** <https://discourse.julialang.org/t/how-to-plot-matrix-as-gradient-grid/82187>\
**Category:** New to Julia\
**Tags:** makie, cairomakie-plotting\
**Created:** [June 3, 2022, 1:57pm UTC](https://discourse.julialang.org/t/how-to-plot-matrix-as-gradient-grid/82187 "2022-06-03T13:57:18Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![Luigi\_Marongiu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/luigi_marongiu/32/7909_2.png) [@Luigi\_Marongiu](https://discourse.julialang.org/u/Luigi_Marongiu)\
**Post date:** [June 3, 2022, 1:57pm UTC](https://discourse.julialang.org/t/how-to-plot-matrix-as-gradient-grid/82187/1 "2022-06-03T13:57:19Z")

</div>

Hello,  
I have a matrix of floating numbers that I would like to plot as a grid whose cells are shaded according to these numbers. Thus, having

```julia
julia> grid = rand(Float32, 5, 5)
5×5 Matrix{Float32}:
 0.157996 0.965474 0.869818 0.741637 0.621585
 0.799489 0.944857 0.243435 0.0732432 0.928711
 0.143084 0.755295 0.153082 0.45804 0.586859
 0.896089 0.772248 0.631192 0.0818451 0.586762
 0.689304 0.0514671 0.73454 0.567052 0.735969

```

I would like to get:

 ![grid](https://global.discourse-cdn.com/julialang/original/3X/b/7/b79a26a077f784bcff0e8f621242c813f2b0d98c.jpeg)

What would be the syntax?  
Thank you

---

<div class="post-metadata">

**Author:** ![icweaver](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/icweaver/32/45764_2.png) [@icweaver](https://discourse.julialang.org/u/icweaver)\
**Post date:** [June 3, 2022, 2:22pm UTC](https://discourse.julialang.org/t/how-to-plot-matrix-as-gradient-grid/82187/2 "2022-06-03T14:22:54Z")

</div>

Oh, do you mean just like a heatmap?

Makie.jl: [heatmap](https://makie.juliaplots.org/v0.17.3/examples/plotting_functions/heatmap/index.html)

Plots.jl: [Heatmap, categorical axes, and aspect\_ratio · Plots](https://docs.juliaplots.org/stable/gallery/gr/generated/gr-ref28/#gr_demo_28)

---

<div class="post-metadata">

**Author:** ![Luigi\_Marongiu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/luigi_marongiu/32/7909_2.png) [@Luigi\_Marongiu](https://discourse.julialang.org/u/Luigi_Marongiu)\
**Post date:** [June 4, 2022, 5:55am UTC](https://discourse.julialang.org/t/how-to-plot-matrix-as-gradient-grid/82187/3 "2022-06-04T05:55:32Z")

</div>

Exactly! Thank you

```julia
using CairoMakie
xs = collect(range(1,stop=5)) 
ys = collect(range(1,stop=5)) 
zs = [cos(x) * sin(y) for x in xs, y in ys]
fig, ax, hm = heatmap(xs, ys, grid)
Colorbar(fig[:, end+1], hm)

```

 ![heatmap](https://global.discourse-cdn.com/julialang/original/3X/6/4/647a560d65148cad4f449e3a2a1e4626f7ca473c.png)

---

<div class="post-metadata">

**Author:** ![icweaver](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/icweaver/32/45764_2.png) [@icweaver](https://discourse.julialang.org/u/icweaver)\
**Post date:** [June 4, 2022, 4:50pm UTC](https://discourse.julialang.org/t/how-to-plot-matrix-as-gradient-grid/82187/4 "2022-06-04T16:50:05Z")

</div>

Nice! CairoMakie is my favorite =]

In case you are interested in doing more stats-oriented plotting, [AlgebraOfGraphics.jl](https://github.com/JuliaPlots/AlgebraOfGraphics.jl) is built-on top of Makie.jl and it [gives you superpowers](http://juliaplots.org/AlgebraOfGraphics.jl/dev/generated/penguins/)

---

<div class="post-metadata">

**Author:** ![jacobusmmsmit](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jacobusmmsmit/32/217669_2.png) [@jacobusmmsmit](https://discourse.julialang.org/u/jacobusmmsmit)\
**Post date:** [June 4, 2022, 5:40pm UTC](https://discourse.julialang.org/t/how-to-plot-matrix-as-gradient-grid/82187/5 "2022-06-04T17:40:50Z")

</div>

You might also like the `aspect`/`Aspect` keyword/object if you want the grid to be squares 🙂

---

<div class="post-metadata">

**Author:** ![Luigi\_Marongiu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/luigi_marongiu/32/7909_2.png) [@Luigi\_Marongiu](https://discourse.julialang.org/u/Luigi_Marongiu)\
**Post date:** [June 5, 2022, 5:00pm UTC](https://discourse.julialang.org/t/how-to-plot-matrix-as-gradient-grid/82187/6 "2022-06-05T17:00:02Z")

</div>

Good point. I tried with

```julia
ax = Axis(fig[1, 1], ...aspect = 1)

```

but it came out rectangular…

```julia
using CairoMakie
using ColorSchemes
xs = collect(range(1,stop=5))
ys = collect(range(1,stop=5))
grid1 = rand(Float16, 5, 5)
grid2 = rand(Float16, 5, 5)
grid = grid1/grid2
fig, ax, hm = heatmap(xs, ys, grid, colormap = :bwr) # or :roma
ax = Axis(fig[1, 1], aspect = 1, xlabel = "X", ylabel = "Y",
    title = "Title")
Colorbar(fig[:, end+1], hm)
save("heatmap.png", fig, px_per_unit = 2)

```

 ![heatmap](https://global.discourse-cdn.com/julialang/original/3X/3/1/31c437fbcd33f6761a5c4ee51c844db0eae88413.png)  
It looks like two plots have merged…

---

<div class="post-metadata">

**Author:** ![icweaver](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/icweaver/32/45764_2.png) [@icweaver](https://discourse.julialang.org/u/icweaver)\
**Post date:** [June 5, 2022, 5:38pm UTC](https://discourse.julialang.org/t/how-to-plot-matrix-as-gradient-grid/82187/7 "2022-06-05T17:38:02Z")

</div>

Yea, so I think there’s a couple things going on here. The merged look is coming from the two separate axes that are being set up (one from `fig, ax, hm = ...`, and the other from `ax = Axis(...)`), so I would just pick one and go with that. If we go with the first way, we can pass a [special keyword arg](https://makie.juliaplots.org/stable/documentation/plot_method_signatures/#special_keyword_arguments)

The other thing that I think is going on is that `aspect = 1` will take care of the aspect ratio of the axis for us, but the figure will still need some special care to get right. There is [a really nice way](https://makie.juliaplots.org/v0.17.3/tutorials/aspect-tutorial/index.html#aspect_ratio_and_size_control_tutorial) to do this that was recently added that I think @jacobusmmsmit was referring to, so I might try something like this:

```julia
xs = 1:5 # Don't think we need to collect here, a UnitRange should be fine
ys = 1:5
grid1 = rand(Float16, 5, 5)
grid2 = rand(Float16, 5, 5)
grid = grid1/grid2

fig, ax, hm = heatmap(xs, ys, grid;
    axis = (xlabel="X", ylabel="Y"),
    colormap = :bwr,
)

Colorbar(fig[:, end+1], hm)

colsize!(fig.layout, 1, Aspect(1, 1.0))
resize_to_layout!(fig)

fig

```

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

---

<div class="post-metadata">

**Author:** ![Luigi\_Marongiu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/luigi_marongiu/32/7909_2.png) [@Luigi\_Marongiu](https://discourse.julialang.org/u/Luigi_Marongiu)\
**Post date:** [June 6, 2022, 6:16am UTC](https://discourse.julialang.org/t/how-to-plot-matrix-as-gradient-grid/82187/8 "2022-06-06T06:16:50Z")

</div>

Perfect! Thank you.

---

<div class="post-metadata">

**Author:** ![Luigi\_Marongiu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/luigi_marongiu/32/7909_2.png) [@Luigi\_Marongiu](https://discourse.julialang.org/u/Luigi_Marongiu)\
**Post date:** [June 9, 2022, 9:21am UTC](https://discourse.julialang.org/t/how-to-plot-matrix-as-gradient-grid/82187/9 "2022-06-09T09:21:37Z")

</div>

An addition: how can I show a grid (to facilitate the discrimination between cells of equal color)? Thanks

---

<div class="post-metadata">

**Author:** ![icweaver](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/icweaver/32/45764_2.png) [@icweaver](https://discourse.julialang.org/u/icweaver)\
**Post date:** [June 9, 2022, 3:01pm UTC](https://discourse.julialang.org/t/how-to-plot-matrix-as-gradient-grid/82187/10 "2022-06-09T15:01:27Z")

</div>

I think this was addressed here [How to add grid lines on top of a heatmap in Makie? - #2 by jules](https://discourse.julialang.org/t/how-to-add-grid-lines-on-top-of-a-heatmap-in-makie/77578/2)

So basically just:

```julia
using CairoMakie

fig = Figure()
ax = Axis(fig[1, 1];
	xgridcolor = :white,
	xgridstyle = :dot,
	xgridwidth = 10,
	ygridcolor = :red,
	ygridwidth = 10,
)

hm = heatmap!(ax, rand(5, 5))

translate!(hm, 0, 0, -100)

fig

```

 ![image](https://global.discourse-cdn.com/julialang/original/3X/f/8/f861b6a3b6c8187666bde57feaefa34aedb0b42c.png)

A bunch more options can be found here [API](https://makie.juliaplots.org/v0.17.3/api/index.html#Axis)

---

<div class="post-metadata">

**Author:** ![Luigi\_Marongiu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/luigi_marongiu/32/7909_2.png) [@Luigi\_Marongiu](https://discourse.julialang.org/u/Luigi_Marongiu)\
**Post date:** [June 9, 2022, 5:20pm UTC](https://discourse.julialang.org/t/how-to-plot-matrix-as-gradient-grid/82187/11 "2022-06-09T17:20:39Z")

</div>

I have seen that, but I am initializing fig, ax, hm in a single call. To me, the lines do not appear:

```julia
using CairoMakie
using ColorSchemes
xs = 1:11
ys = 1:11
grid1 = rand(Float16, 11, 11)
grid2 = rand(Float16, 11, 11)
grid = grid1/grid2
fig, ax, hm = heatmap(xs, ys, grid;
    axis = (xlabel="X", ylabel="Y", xgridcolor = :black, ygridcolor = :black),
    colormap = :bwr)
Colorbar(fig[:, end+1], hm)
colsize!(fig.layout, 1, Aspect(1, 1.0))
resize_to_layout!(fig)
save("heatmap.png", fig, px_per_unit = 2)

```

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://discourse.julialang.org/u/jules)\
**Post date:** [June 9, 2022, 7:10pm UTC](https://discourse.julialang.org/t/how-to-plot-matrix-as-gradient-grid/82187/12 "2022-06-09T19:10:07Z")

</div>

You don’t have the line `translate!(hm, 0, 0, -100)`, just add that after the heatmap call.

---

<div class="post-metadata">

**Author:** ![Luigi\_Marongiu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/luigi_marongiu/32/7909_2.png) [@Luigi\_Marongiu](https://discourse.julialang.org/u/Luigi_Marongiu)\
**Post date:** [June 9, 2022, 9:00pm UTC](https://discourse.julialang.org/t/how-to-plot-matrix-as-gradient-grid/82187/13 "2022-06-09T21:00:47Z")

</div>

> [@jules](#):
>
> translate!(hm, 0, 0, -100)

Yep, thank you!
