# Plot a raster matrix with national borders from a shape file?

**URL:** <https://discourse.julialang.org/t/plot-a-raster-matrix-with-national-borders-from-a-shape-file/23258>\
**Category:** General Usage\
**Created:** [April 18, 2019, 1:57am UTC](https://discourse.julialang.org/t/plot-a-raster-matrix-with-national-borders-from-a-shape-file/23258 "2019-04-18T01:57:37Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Raf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raf/32/3383_2.png) [@Raf](https://discourse.julialang.org/u/Raf)\
**Post date:** [April 18, 2019, 1:57am UTC](https://discourse.julialang.org/t/plot-a-raster-matrix-with-national-borders-from-a-shape-file/23258/1 "2019-04-18T01:57:37Z")

</div>

I have a matrix of raster data, the original NetCDF file with projection data etc, and a shape file.

Is there an easy way to accurately plot the matrix as a heatmap with a border supplied by the shapefile?

---

<div class="post-metadata">

**Author:** ![Raf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raf/32/3383_2.png) [@Raf](https://discourse.julialang.org/u/Raf)\
**Post date:** [April 23, 2019, 5:11am UTC](https://discourse.julialang.org/t/plot-a-raster-matrix-with-national-borders-from-a-shape-file/23258/2 "2019-04-23T05:11:20Z")

</div>

To answer my own question for anyone else needing this:

```julia
using NetCDF, Shapefile, Plots

matrx = your_matrix
shapepath = "path/to/file.shp"
ncdfpath = "path/to/file.nc"

shp = open(shapepath) do io
    read(io, Shapefile.Handle)
end

# Get lat and long from the original NetCDF
long = ncread(ncdfpath, "longitude")
lat = ncread(ncdfpath, "latitude")
# Use a `rotXXX()` function to fix the orientation if required, 
# then plot a heatmap with correct long and lat
plt = heatmap(long, lat, rotl90(matrx))
# Then overlay the shape, specifying the xlim and ylim or the matrix
plot!(plt, shp.shapes, xlim=(long[1], long[end]), ylim=(lat[end], lat[1]))

```
