# Color the lines in a contour plot of \`z\` by the values a different matrix \`w\`

**URL:** <https://discourse.julialang.org/t/color-the-lines-in-a-contour-plot-of-z-by-the-values-a-different-matrix-w/51233>\
**Category:** Visualization\
**Tags:** plotting\
**Created:** [December 4, 2020, 2:13am UTC](https://discourse.julialang.org/t/color-the-lines-in-a-contour-plot-of-z-by-the-values-a-different-matrix-w/51233 "2020-12-04T02:13:12Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![ash](https://avatars.discourse-cdn.com/v4/letter/a/f19dbf/32.png) [@ash](https://discourse.julialang.org/u/ash)\
**Post date:** [December 4, 2020, 2:13am UTC](https://discourse.julialang.org/t/color-the-lines-in-a-contour-plot-of-z-by-the-values-a-different-matrix-w/51233/1 "2020-12-04T02:13:12Z")

</div>

Let’s say I have the following MWE

```julia
using Plots

x = y = range(0, stop=π, length=100)

z = cos.(x) .+ sin.(y')
w = cos.(x).^2 .- sin.(y').^2

contour(x,y,z, levels=1)

```

I would like the lines from the contour plot of `z` to be coloured using the values of `w`. Is this possible in some way?

One approach I tried is extracting the contours manually using `Contour.jl` but I got stuck on how to extract the contour mask to apply to `w` so I can use the `line_z` kwarg. (see [Contour.jl: extract the indices of the contour](https://discourse.julialang.org/t/contour-jl-extract-the-indices-of-the-contour/51229))

Thoughts? I would love to do this using `Plots.jl` since that’s what my entire code base is based on but I am open to solutions since I have been stuck on this for a while now.

---

<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:** [January 30, 2022, 11:43am UTC](https://discourse.julialang.org/t/color-the-lines-in-a-contour-plot-of-z-by-the-values-a-different-matrix-w/51233/2 "2022-01-30T11:43:51Z")

</div>

The following works for `w = w(x,y)`, but if you have only a numerical matrix `w` then I think you could define an interpolator function of `(x,y)` to be called:

```julia
using Contour, Plots; gr(dpi=600)

x = y = range(0, π, 100)
z = @. cos(x) + sin(y')
w(x,y) = @. cos(x)^2 - sin(y')^2

plot()
for cl in levels(contours(x,y,z))
    for line in lines(cl)
        xs, ys = coordinates(line) # coordinates of each contour line
        plot!(xs, ys, st=:path, markershape=:none, line_z = w(xs,ys)) # use plotting package you prefer
    end
end
Plots.current()

```

 ![Contour_Plots_line_z_colored_contours](https://global.discourse-cdn.com/julialang/original/3X/4/f/4f85489322f448ce0d8376e917dde705fef16a34.png)
