# Positioning \`contour\_labels\` for contour plots

**URL:** <https://discourse.julialang.org/t/positioning-contour-labels-for-contour-plots/101265>\
**Category:** Visualization\
**Tags:** plots, gr\
**Created:** [July 6, 2023, 3:08pm UTC](https://discourse.julialang.org/t/positioning-contour-labels-for-contour-plots/101265 "2023-07-06T15:08:42Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Gregstrq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gregstrq/32/20620_2.png) [@Gregstrq](https://discourse.julialang.org/u/Gregstrq)\
**Post date:** [July 6, 2023, 3:08pm UTC](https://discourse.julialang.org/t/positioning-contour-labels-for-contour-plots/101265/1 "2023-07-06T15:08:42Z")

</div>

Folks, I am using Plots.jl with GR backend.  
Basically, what I am trying to do is to draw the lines of a 2d curvilinear coordinate system.  
Specifically, I do two contour plots on the same axes, that give me two sets of intersecting lines.

If I try to enable `contour_labels=true` for both contour plots, then everything gets very ugly.  
Is there a way to adjust finely the positioning of the `clabels`? I mean, specify, which levels should have a label, and also specify, whether to put the label in the middle of the curve or at the edge?

---

<div class="post-metadata">

**Author:** ![jheinen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jheinen/32/2787_2.png) [@jheinen](https://discourse.julialang.org/u/jheinen)\
**Post date:** [July 6, 2023, 6:37pm UTC](https://discourse.julialang.org/t/positioning-contour-labels-for-contour-plots/101265/2 "2023-07-06T18:37:32Z")

</div>

Could you provide an “ugly” example?

---

<div class="post-metadata">

**Author:** ![Gregstrq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gregstrq/32/20620_2.png) [@Gregstrq](https://discourse.julialang.org/u/Gregstrq)\
**Post date:** [July 7, 2023, 10:01am UTC](https://discourse.julialang.org/t/positioning-contour-labels-for-contour-plots/101265/3 "2023-07-07T10:01:00Z")

</div>

Here is one:  
 ![ugly-plot](https://global.discourse-cdn.com/julialang/original/3X/5/4/54cd1ff2efc4c31578eee3c3f16e613c487d5d35.png)

---

<div class="post-metadata">

**Author:** ![jheinen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jheinen/32/2787_2.png) [@jheinen](https://discourse.julialang.org/u/jheinen)\
**Post date:** [July 9, 2023, 4:28am UTC](https://discourse.julialang.org/t/positioning-contour-labels-for-contour-plots/101265/4 "2023-07-09T04:28:25Z")

</div>

The position of the labels is determined per contour lot. So in principle you can’t put them on top of each other in a meaningful way. Mixing different notations for floating point numbers should of course also be avoided.

Nevertheless, something is wrong with the text positioning in this example. Therefore it would be useful to see the code.

---

<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:** [July 9, 2023, 9:52am UTC](https://discourse.julialang.org/t/positioning-contour-labels-for-contour-plots/101265/5 "2023-07-09T09:52:31Z")

</div>

Try [Contour.jl](https://juliageometry.github.io/Contour.jl/stable/tutorial.html) and use any Plots backend or other plotting package. `Contour.jl` lets you access individual contour data, but you’ll need an algorithm to automate labeling, which doesn’t seem trivial…

A naive example using Contour.jl, Plots.jl and gr():

 ![Plots_Double_Contours_manual_labels](https://global.discourse-cdn.com/julialang/original/3X/5/c/5c4b91eaa94d9e81c5e508928c71157ca9237c16.jpeg)

---

<div class="post-metadata">

**Author:** ![joa-quim](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joa-quim/32/227_2.png) [@joa-quim](https://discourse.julialang.org/u/joa-quim)\
**Post date:** [July 9, 2023, 10:22am UTC](https://discourse.julialang.org/t/positioning-contour-labels-for-contour-plots/101265/6 "2023-07-09T10:22:18Z")

</div>

The GMT [contour](https://www.generic-mapping-tools.org/GMTjl_doc/documentation/modules/contour/index.html#contour), [grdcontour](https://www.generic-mapping-tools.org/GMTjl_doc/documentation/modules/grdcontour/index.html#grdcontour) and [contourf](https://www.generic-mapping-tools.org/GMTjl_doc/documentation/modules/contourf/index.html#contourf) modules let control everything one reasonably want.

---

<div class="post-metadata">

**Author:** ![Gregstrq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gregstrq/32/20620_2.png) [@Gregstrq](https://discourse.julialang.org/u/Gregstrq)\
**Post date:** [July 10, 2023, 11:02am UTC](https://discourse.julialang.org/t/positioning-contour-labels-for-contour-plots/101265/7 "2023-07-10T11:02:19Z")

</div>

> [@joa-quim](#):
>
> The GMT [contour](https://www.generic-mapping-tools.org/GMTjl_doc/documentation/modules/contour/index.html#contour), [grdcontour](https://www.generic-mapping-tools.org/GMTjl_doc/documentation/modules/grdcontour/index.html#grdcontour) and [contourf](https://www.generic-mapping-tools.org/GMTjl_doc/documentation/modules/contourf/index.html#contourf) modules let control everything one reasonably want.

I am doing the figures for the paper, and I would like them to look uniform. And I don’t want to remake all other figures to switch to a new backend.

> [@jheinen](#):
>
> The position of the labels is determined per contour lot. So in principle you can’t put them on top of each other in a meaningful way. Mixing different notations for floating point numbers should of course also be avoided.

That is why I am asking whether there is a way for a fine control over contour\_label. That is: how many labels per line, where to put them, how are they styled (provide custom clabels, adjust fontsize, etc.), disable labels for some of the levels.

---

<div class="post-metadata">

**Author:** ![jheinen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jheinen/32/2787_2.png) [@jheinen](https://discourse.julialang.org/u/jheinen)\
**Post date:** [July 11, 2023, 7:18am UTC](https://discourse.julialang.org/t/positioning-contour-labels-for-contour-plots/101265/8 "2023-07-11T07:18:26Z")

</div>

~~Unfortunately, currently you can only specify the contour levels as an interval - discrete values are not possible.~~ A possible workaround would be repeated calls with a single height value, but this would again cause problems regarding the positioning.

The type and size of the font, on the other hand, could be implemented with relatively little effort. But that does not solve your problem …

---

<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:** [July 11, 2023, 7:58am UTC](https://discourse.julialang.org/t/positioning-contour-labels-for-contour-plots/101265/9 "2023-07-11T07:58:29Z")

</div>

> [@jheinen](#):
>
> Unfortunately, currently you can only specify the contour levels as an interval - discrete values are not possible

Discrete contour levels seem to work:

```julia
using Plots; gr(dpi=600)
f(x, y) = sin(x*y)
Plots.contour(x, y, f, contour_labels=true, levels=[-0.6, 0, 0.4])

```
