# How to plot a gridworld policy in Makie

**URL:** <https://discourse.julialang.org/t/how-to-plot-a-gridworld-policy-in-makie/111217>\
**Category:** General Usage\
**Tags:** question, plotting\
**Created:** [March 5, 2024, 8:48pm UTC](https://discourse.julialang.org/t/how-to-plot-a-gridworld-policy-in-makie/111217 "2024-03-05T20:48:30Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![00krishna](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/00krishna/32/8843_2.png) [@00krishna](https://discourse.julialang.org/u/00krishna)\
**Post date:** [March 5, 2024, 8:48pm UTC](https://discourse.julialang.org/t/how-to-plot-a-gridworld-policy-in-makie/111217/1 "2024-03-05T20:48:30Z")

</div>

Hey folks. I was looking at a [notebook](https://github.com/JuliaPOMDP/POMDPExamples.jl/blob/master/notebooks/GridWorld.ipynb) created by the POMDP folks for plotting the Value Iteration policy for a simple Gridworld.

I have a copy of the picture below. I was wondering how would I plot something like this in Makie. From the image it seems like this is a makie plot.

 ![gw](https://global.discourse-cdn.com/julialang/original/3X/9/e/9ed787b3c80159ca81787e9f199a2f058168dc86.jpeg)

I imagine I would use the `arrows()` function to create the actual arrows, but I was not sure about a couple of things.

1. How would I setup the grid so that there is only one arrow per grid cell?
2. If I add an obstacle to the grid, how do I color only specific grid cells while leaving the others untouched?

Eventually I would like to create a plot similar to this, which comes from one of Russ Tedrake’s excellent [Underactuated Robotics](https://underactuated.mit.edu/dp.html#grid_world) only textbook.

 ![Selection_001](https://global.discourse-cdn.com/julialang/original/3X/b/4/b4d35204309123ab95864e09e91aee911b6b0e00.png)

I get that value iteration does not work in more than a few dimensions, so it is not tractable for real world problems. But some of these simple visualizations help to reinforce the intuition behind the algorithms and also debug some code.

Thanks.

---

<div class="post-metadata">

**Author:** ![sijo](https://avatars.discourse-cdn.com/v4/letter/s/da6949/32.png) [@sijo](https://discourse.julialang.org/u/sijo)\
**Post date:** [March 6, 2024, 11:19am UTC](https://discourse.julialang.org/t/how-to-plot-a-gridworld-policy-in-makie/111217/2 "2024-03-06T11:19:10Z")

</div>

For the grid you can just give arrow positions consistent with the cells? And for obstacles, you can change the cell value to a specific “obstacle” value? (or draw on top of the grid with another plot call). For example:

```julia
using CairoMakie

x = 0:20
y = 0:15
z = @. (x-2)^2 + (y'-8)^2
d = rand([(0,0.5), (0,-0.5), (0.5,0), (-0.5,0)], size(z))

# Add some obstacle
z[3:4, 2:8] .= -100

fig = Figure()
ax = Axis(fig[1,1], xticks=x, yticks=y)
heatmap!(x, y, z)
arrows!(x, y, first.(d), last.(d))
fig

```

 ![image](https://global.discourse-cdn.com/julialang/original/3X/b/2/b2d29e29735506f1889e0bbdcb151c5874d67c09.png)

---

<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:** [March 6, 2024, 6:16pm UTC](https://discourse.julialang.org/t/how-to-plot-a-gridworld-policy-in-makie/111217/3 "2024-03-06T18:16:08Z")

</div>

You can use `NaN` and `nan_color` for a border object:

```julia
using CairoMakie

x = 0:20
y = 0:15
z = @. (x-2)^2 + (y'-8)^2 |> Float32
d = rand([(0,0.5), (0,-0.5), (0.5,0), (-0.5,0)], size(z))

# Add some obstacle
z[3:4, 2:8] .= NaN

fig = Figure()
ax = Axis(fig[1,1], xticks=x, yticks=y)
heatmap!(x, y, z, nan_color = :red)
arrows!(x, y, first.(d), last.(d))
fig

```

 ![image](https://global.discourse-cdn.com/julialang/original/3X/5/c/5c23e768d73fdf893c10cd05c93006bbf07f95ed.png)
