# How to save a heatmap as a png?

**URL:** <https://discourse.julialang.org/t/how-to-save-a-heatmap-as-a-png/22738>\
**Category:** Visualization\
**Created:** [April 4, 2019, 8:56am UTC](https://discourse.julialang.org/t/how-to-save-a-heatmap-as-a-png/22738 "2019-04-04T08:56:30Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![HenrikM](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrikm/32/17975_2.png) [@HenrikM](https://discourse.julialang.org/u/HenrikM)\
**Post date:** [April 4, 2019, 8:56am UTC](https://discourse.julialang.org/t/how-to-save-a-heatmap-as-a-png/22738/1 "2019-04-04T08:56:30Z")

</div>

I can plot a matrix as a heatmap and I can even choose my own colormap. But how can I save just the heatmap (not the plot) as a png-file? For example:

```julia
using Plots
gr()

xs = range(-1, 1, length=30)
ys = range(-1, 1, length=30)
zs = randn(30, 30)
whiteblue = cgrad([:white, :blue])
heatmap(xs, ys, zs, colorbar=false, color=whiteblue)

```

Is there any way to save the matrix as a png, possibly with a custom colormap? I can do

```julia
using Images
using FileIO
save(File(format"PNG", "map.png"), colorview(Gray, zs))

```

Is this even close that what I want? Can I change the colormap? I would prefer to have the same colormap as in the heatmap plot, but that is not strictly necessary, something similar is also fine. Maybe I just failed to spot it in the docs.

---

<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:** [April 4, 2019, 10:31am UTC](https://discourse.julialang.org/t/how-to-save-a-heatmap-as-a-png/22738/2 "2019-04-04T10:31:03Z")

</div>

```julia
help?> savefig
search: savefig StackOverflowError

  savefig([plot,] filename)

  Save a Plot (the current plot if plot is not passed) to file. The file type is inferred from the file extension. All backends support png and pdf file types, some
  also support svg, ps, eps, html and tex.

```

---

<div class="post-metadata">

**Author:** ![HenrikM](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrikm/32/17975_2.png) [@HenrikM](https://discourse.julialang.org/u/HenrikM)\
**Post date:** [April 4, 2019, 10:38am UTC](https://discourse.julialang.org/t/how-to-save-a-heatmap-as-a-png/22738/3 "2019-04-04T10:38:39Z")

</div>

Sorry for being unclear. I don’t want to save the Plot. I want to just save the heatmap (like, the picture that is inside the axis). No axis, no padding, just the heatmap.

---

<div class="post-metadata">

**Author:** ![Jon\_Norberg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jon_norberg/32/3218_2.png) [@Jon\_Norberg](https://discourse.julialang.org/u/Jon_Norberg)\
**Post date:** [April 4, 2019, 11:32am UTC](https://discourse.julialang.org/t/how-to-save-a-heatmap-as-a-png/22738/4 "2019-04-04T11:32:30Z")

</div>

Look into Images.jl

Create image from your matrix. Of course you need to make your own colorscale

---

<div class="post-metadata">

**Author:** ![HenrikM](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrikm/32/17975_2.png) [@HenrikM](https://discourse.julialang.org/u/HenrikM)\
**Post date:** [April 4, 2019, 11:42am UTC](https://discourse.julialang.org/t/how-to-save-a-heatmap-as-a-png/22738/5 "2019-04-04T11:42:00Z")

</div>

Ok, thanks! I thought that the `colorview` was exactly for that, but I should look for something else then? What is a colorscale (apparently something else than `cgrad`)? Where do I find documentation for that?

---

<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:** [April 4, 2019, 11:54am UTC](https://discourse.julialang.org/t/how-to-save-a-heatmap-as-a-png/22738/6 "2019-04-04T11:54:34Z")

</div>

You could also do the plot without any axes etc. and either adjust the axis-size to the figure size, or use some command-line tool to crop the white border.
