# Colorbar of a Contour Plot

**URL:** <https://discourse.julialang.org/t/colorbar-of-a-contour-plot/131566>\
**Category:** Visualization\
**Tags:** plots\
**Created:** [August 12, 2025, 4:30pm UTC](https://discourse.julialang.org/t/colorbar-of-a-contour-plot/131566 "2025-08-12T16:30:11Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![prconlin](https://avatars.discourse-cdn.com/v4/letter/p/b19c9b/32.png) [@prconlin](https://discourse.julialang.org/u/prconlin)\
**Post date:** [August 12, 2025, 4:30pm UTC](https://discourse.julialang.org/t/colorbar-of-a-contour-plot/131566/1 "2025-08-12T16:30:11Z")

</div>

Hello,

I have a matrix of discrete values, and I would like to plot it as a contour plot instead of a heatmap, because reasons. This works well enough, except that the colorbar is strange and I can’t find a way to correct it.

The MWE:

```julia-auto
using Plots # gr
x = rand(1:10, 100, 100)
contour(x)

```

![Contour](https://global.discourse-cdn.com/julialang/original/3X/7/e/7ec83639c4e1a0a297dba996b0de54268faf3b06.svg)  
For my use case, a discrete colorbar with bins would be ideal (see [here](https://discourse.julialang.org/t/plots-jl-how-can-i-get-discrete-colors-in-the-colorbar-of-a-heatmap/29660/4) for reference). I would also be fine with a continuous colorbar, which is the default for heatmaps in GR. But instead I get this strange bar with discrete levels that do not correspond to the step size. Moreover, the bins are not colored in. Using `contourf()` or `fill = true` fills in the bins, but they still do not correspond to the step size.

I’ve tried playing around with the following attributes, without success:  
`levels`, `colorbar_formatter`, `colorbar_ticks`, and `colorbar_continous_values`

Any help would be appreciated,  
Thanks

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [August 12, 2025, 5:16pm UTC](https://discourse.julialang.org/t/colorbar-of-a-contour-plot/131566/2 "2025-08-12T17:16:30Z")

</div>

> [@prconlin](#):
>
> `contour(x)`

Try this:

```julia-auto
contourf(x, levels=1:10)

```

---

<div class="post-metadata">

**Author:** ![prconlin](https://avatars.discourse-cdn.com/v4/letter/p/b19c9b/32.png) [@prconlin](https://discourse.julialang.org/u/prconlin)\
**Post date:** [August 12, 2025, 6:46pm UTC](https://discourse.julialang.org/t/colorbar-of-a-contour-plot/131566/3 "2025-08-12T18:46:09Z")

</div>

Thanks for the suggestion, it works!

Ideally, I would like to fill the colorbar without filling in the contour plot, but I don’t expect that is possible.

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [August 12, 2025, 7:02pm UTC](https://discourse.julialang.org/t/colorbar-of-a-contour-plot/131566/4 "2025-08-12T19:02:18Z")

</div>

> [@prconlin](#):
>
> but I don’t expect that is possible.

You can use the tired trick with layouts:

```julia-auto
l = @layout [a{0.9w} b]
p1 = contour(x, levels=1:10, colorbar=false)
p2 = contourf(NaN*x, levels=1:10, framestyle=:none, legend=false, clims=extrema(x), colorbar=true)
plot(p1, p2, layout=l)

```
