# Contour plot data extraction and replacing z slices with user defined function

Let's say we have a contour plot of the following,

ContourPlot[Cos[x] + Cos[y], {x, 0, Pi}, {y, 0, Pi},
ContourLabels -> True]


that gives,

we can extract the line objects and points associated with those lines as

Cases[Normal@%, Line[pts_] -> pts, Infinity]

ListLinePlot@%


which looks like in the following,

so, is there any way I can replace the line points, which are infact z slices with a user-defined function Sin[z]? so it will be Sin[-1.5],Sin[-1] and so on

and plot the contour plot again.

so extraction of data from the contour plot and changing it with a user-defined function

• Is z the contourvalue? Commented Feb 28, 2023 at 12:40
• @Ulrich Neumann yes!
– a019
Commented Feb 28, 2023 at 12:52
• Just to be clear, are you trying to rescale the contours lines? So rather than the contour lines indicating points where the amplitude z = 1.5 for example, you prefer it to instead show points where the amplitude is Sin[1.5]?
– alex
Commented Feb 28, 2023 at 13:03
• @alex Yes, rescaling z=1.5 with a function Sin[z] or Sin[1.5], so kind of replacing as well.
– a019
Commented Feb 28, 2023 at 13:04

Try

plot = ContourPlot[Cos[x] + Cos[y], {x, 0, Pi}, {y, 0, Pi} ]


detect contourlines and values

list = Cases[Normal@plot, Tooltip[{__, a__Line }, b_] :> {b, a  },Infinity];


create new points Sin[conourvalue]

p3D = Flatten[
Table[Map[Join[#, {Sin[list[[i, 1]]]}] &, list[[i, 2]][[1 ]]], {i,Length[list]}] , 1]
ListPlot3D[p3D, MeshFunctions -> {#3 &}]


• can we plot p3D in 2D contourplot rather than in Graphics3D?
– a019
Commented Feb 28, 2023 at 13:18
• @a019 I modified my answer Commented Feb 28, 2023 at 13:21

Manipulating the list as shown by @Ulrich is what one would do to simply rescale.

For the sake of completion, I'm also adding the option in case you wanted to know the contour points for a specified function. You can achieve this using ContourPlot3D:

ContourPlot3D[
Cos[x] + Cos[y] == Sin[z], {x, 0, Pi}, {y, 0, Pi}, {z, -2, 2}]


You of course recover your default answers by leaving the equation unspecified:

ContourPlot3D[Cos[x] + Cos[y], {x, 0, Pi}, {y, 0, Pi}, {z, -2, 2},
Contours -> 11]


Best of luck.