# Plots.contour rounding the contour labels

**URL:** <https://discourse.julialang.org/t/plots-contour-rounding-the-contour-labels/100939>\
**Category:** Visualization\
**Tags:** plotting, plots\
**Created:** [June 28, 2023, 4:39pm UTC](https://discourse.julialang.org/t/plots-contour-rounding-the-contour-labels/100939 "2023-06-28T16:39:25Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![selvi-aras](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/selvi-aras/32/50637_2.png) [@selvi-aras](https://discourse.julialang.org/u/selvi-aras)\
**Post date:** [June 28, 2023, 4:39pm UTC](https://discourse.julialang.org/t/plots-contour-rounding-the-contour-labels/100939/1 "2023-06-28T16:39:25Z")

</div>

I am using `Plots.jl` to generate a contour plot. It works great but the contour labels have 5 digits after the decimal point:  
 ![image](https://global.discourse-cdn.com/julialang/original/3X/f/3/f3fd0f8af7e30e903d9394fb31690830ea326392.png)

How can I update my code, which is shared next, so that the contour labels are rounded to decimals? I looked into this for a while, and there is no easy way so far. I would prefer an option that does **not** use any other visualisation package than `Plots.jl`. Thank you.

```julia
x_ran = range(-4, 2.0, length=600)
y_ran = range(-1, 2.5, length=600)
z = @. eval_f(x_ran', y_ran)
Plots.contour(x_ran, y_ran, z, levels=15, 
        color=:acton, xaxis = (L"x_1",font(10)), 
        yaxis = (L"x_2", font(10)), clabels= true,
        title = "contour plot",
        cbar=false, lw=1, 
        xguidefontsize=14, yguidefontsize=14)

```

---

<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:** [June 28, 2023, 6:23pm UTC](https://discourse.julialang.org/t/plots-contour-rounding-the-contour-labels/100939/2 "2023-06-28T18:23:32Z")

</div>

For your example, try passing to `contour()` the keyword argument:  
`levels=4:13`

---

<div class="post-metadata">

**Author:** ![selvi-aras](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/selvi-aras/32/50637_2.png) [@selvi-aras](https://discourse.julialang.org/u/selvi-aras)\
**Post date:** [June 29, 2023, 8:44am UTC](https://discourse.julialang.org/t/plots-contour-rounding-the-contour-labels/100939/3 "2023-06-29T08:44:19Z")

</div>

Thanks! Good hack!

---

<div class="post-metadata">

**Author:** ![sdanisch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sdanisch/32/1406_2.png) [@sdanisch](https://discourse.julialang.org/u/sdanisch)\
**Post date:** [July 13, 2023, 9:28am UTC](https://discourse.julialang.org/t/plots-contour-rounding-the-contour-labels/100939/4 "2023-07-13T09:28:52Z")

</div>

In Makie you can pass a labelformatter:

```julia
using CairoMakie, Printf
using Makie.LaTeXStrings
eval_f(x, y) = cos(x) * sin(y)
x_ran = range(-4, 2.0, length=600)
y_ran = range(-1, 2.5, length=600)
z = @. eval_f(x_ran', y_ran)

contour(x_ran, y_ran, z, levels=15,
    labels=true, labelformatter=(x) -> @sprintf("%.1f", x),
    colormap=:acton, axis=(xlabel=L"x_1", ylabel=L"x_2", title="contour plot"),
)

```

 ![test](https://global.discourse-cdn.com/julialang/original/3X/e/1/e12c2ac10517f07bca52b8f303c162d032def06f.png)
