Mathematica 9 is now available
Student Support Forum
-----
Student Support Forum: 'overlay of a surface graph and a contourn graph' topicStudent Support Forum > General > "overlay of a surface graph and a contourn graph"

< Previous CommentHelp | Reply To Comment | Reply To Topic
Author Comment/Response
Forum Moderator
email me
09/29/98 3:20pm

>
> I am trying to graph a surface graph and the corresponding contourn graph in the same box. HOwever,I alway get the same error, saying that the Nlist contain only two numbers when three are needed. Let me give you my instructions
>
> myCP= ContourPlot[uapproxim[x, y], {x, 0, L}, {y, 0, M}, Contours -> Union[Table[i, {i, 0, 1/4, 1/44}], Table[i, {i, 1/4, 3/2, 1/8}]], PlotPoints -> 30, AspectRatio -> Automatic, PlotRange -> All, ContourLines -> True, ContourShading -> True, ContourStyle -> (Map[{Hue[#, 1, Random[]], Thickness[.006]}&, Range[0, 1, 1/12]]), ColorFunction -> Hue, Ticks -> {Range[0, 1], Range[0, 2], {-1.5, 1.5}}, DisplayFunction -> Identity]
>
> myContourGP= First@Graphics@myCP;
> myContourGP= N@myContourGP/.{x_AtomQ, y_AtomQ} -> {x, y, -20};
>
> Show[{SurfaceGraphics@myCP,Graphics3D@myContourGP}, Axes -> True, BoxRatios -> {1, 1, 1}, DisplayFunction -> $DisplayFunction]
>
> the rest of the instructions follow:
> L:= 1
> M:= 2
> l[n_]:= n Pi/L//N
> m[n_]:= n Pi/M//N
>
> f1[x_]:= x^2
> f2[x_]:= x+2
> g1[y_]:= y
> g2[y_]:= y+1
>
> A[n_]:=
> A[n] = 2/L NIntegrate[f1[x] Sin[l[n] x], {x, 0, L}]//Chop;
>
> B[n_]:=
> B[n] = 1/Sinh[l[n] M] (2/L NIntegrate[f2[x] Sin[l[n] x], {x, 0, L}] - A[n] Cosh[l[n] M])//Chop;
>
> Table[{n, A[n], B[n]}, {n, 1, 8}]//ColumnForm
> u1[x_, y_, n_]:= (A[n] Cosh[l[n] y] + B[n] Sinh[l[n] y]) Sin[l[n] x]
> u1approx[x_, y_]:= Sum[u1[x, y, n], {n, 8}]
> threeDplot1 = Plot3D[u1approx[x, y], {x, 0, L}, {y, 0, M}, DisplayFunction-> Identity];
> Show[threeDplot1, DisplayFunction->$DisplayFunction]
> \[SkeletonIndicator]SurfaceGraphics\[SkeletonIndicator]
> cvals1:= Table[i, {i, 0, 1/4, 1/44}]
> cvals2:= Table[i, {i, 1/4, 3/2, 5/44}]
> contourvals:= Union[cvals1, cvals2]
> u1[x_, y_, n_]:= (A[n] Cosh[l[n] y] + B[n] Sinh[l[n] y]) Sin[l[n] x]
> u1approx[x_, y_]:= Sum[u1[x, y, n], {n, 8}]
> contourgraphs1 = ContourPlot[u1approx[x, y], {x, 0, 1}, {y, 0, 2}, PlotPoints -> 40, Contours -> contourvals, ContourShading -> False, DisplayFunction -> Identity];
> Show[GraphicsArray[{threeDplot1, contourgraphs1}], DisplayFunction -> $DisplayFunction]
> \[SkeletonIndicator]GraphicsArray\[SkeletonIndicator]
> a[n_]:= a[n] = 2/M NIntegrate[g1[y] Sin[m[n] y], {y, 0, M}]//Chop
> b[n_]:= b[n] = 1/Sinh[m[n] L] (2/M NIntegrate[g2[y] Sin[m[n] y], {y, 0, M}] - a[n] Cosh[m[n] L])//Chop
> u2[x_, y_, n_]:= (a[n] Cosh[m[n] x] + b[n] Sinh[m[n] x])* Sin[m[n] y]
> u2approx[x_, y_]:= Sum[u2[x, y, n], {n, 1, 8}]
> threeDplot2 = Plot3D[u2approx[x, y], {x, 0, L}, {y, 0, M}, DisplayFunction -> Identity];
> threeDplot1 = Plot3D[u1approx[x, y], {x, 0, L}, {y, 0, M}, DisplayFunction-> Identity];
> Show[threeDplot1, DisplayFunction->$DisplayFunction]
> \[SkeletonIndicator]SurfaceGraphics\[SkeletonIndicator]
> contourgraphs2= ContourPlot[u2approx[x, y], {x, 0, 1}, {y, 0, 2}, PlotPoints -> 40, Contours -> contourvals, ContourShading -> False, DisplayFunction -> Identity];
> Show[GraphicsArray[{threeDplot2, contourgraphs2}], DisplayFunction -> $DisplayFunction]
> \[SkeletonIndicator]GraphicsArray\[SkeletonIndicator]
> uapproxim[x_, y_]:= u1approx[x, y]+ u2approx[x, y];
> threeDplot= Plot3D[uapproxim[x, y], {x, 0, L}, {y, 0, M}, DisplayFunction -> Identity];
> contourgraphsu= ContourPlot[uapproxim[x, y], {x, 0, 1}, {y, 0, 2}, PlotPoints -> 40, Contours -> contourvals, ContourShading -> False, DisplayFunction -> Identity];
> Show[GraphicsArray[{threeDplot, contourgraphsu}], DisplayFunction -> $DisplayFunction]
> \[SkeletonIndicator]GraphicsArray\[SkeletonIndicator]
>
> I need urgent help.
> Thanks
>

============

I would guess that the error message that you are seeing is the result of trying to display something that has only 2 coordinates in 3 space. Since you are familiar with your code, you can probably find what that entity is and where the attempt occurs.

Here is one way to overlay contour lines onto a surface plot. Suppose you have a plot of a surface:

In[33]:=
gr=Plot3D[ w^2 - Cos[v], {v, -Pi, Pi},{w, -1.5, 1.5} ]
Out[33]= -SurfaceGraphics-

You can get contour lines over laying a surface in a number of ways. This one seems the most straight forward to my way of thinking. It hinges on converting the SurfaceGraphics expression to a ContourGraphics expression.

You can convert any SurfaceGraphics expression to a ContourGraphics expression directly and show the result:

In[34]:= ctgr = Show[ContourGraphics[gr]]
Out[34]= -ContourGraphics-

The shading can also be turned off. It is this graphic that we will extract the lines from.

In[35]:= ctgrlines = Show[ContourGraphics[gr], ContourShading->False]
Out[35]= -ContourGraphics-

Now convert this ContourGraphics expression to a Graphics expression. That is done in the same way as one gets the ContourGraphics expression from the 3D plot..

In[36]:= listOlines = Graphics[ctgrlines];

listOlines consists of a list of lists of Graphics Line primitives and Graphics options (PlotRange -> Automatic and so on). If you wish to see the whole expression, you can execute InputForm[listOlines]. In this case we are mainly interest in the Line primitives, which are in the first Part of the list, i.e. Part 1 ( [[1]] ) of the list.

In[38]:= listOlines[[1]]

Out[38]=
\!\({{GrayLevel[0.`], AbsoluteThickness[0.5`],
Line[{{3.14159265358979311`, 1.39573828519492582`}, {
2.69279370307696552`, 1.4307140476875193`}, {
2.29077586012866873`, 1.49999999999999955`}}]}, \n\t\n
\t (*\ Many\ more\ lists\ like\ these\ *) \n\t\n
\t{GrayLevel[0.`], AbsoluteThickness[0.5`],
Line[{{\(-0.448798950512827587`\), \(-0.449248388682410482`\)}, {0.`,
\(-0.553064459301436706`\)}, {0.448798950512827587`,
\(-0.449248388682410482`\)}, {0.505066313065112026`,
\(-0.428571428571428736`\)}, {0.802332238162222921`,
\(-0.214285714285714457`\)}, {0.85081679346112633`,
\(-1.66533453693773481`*^-16\)}, {0.802332238162223276`,
0.214285714285714101`}, {0.505066313065112915`,
0.428571428571428381`}, {0.448798950512827587`,
0.449248388682410038`}, {0.`, 0.55306445930143635`}, {
\(-0.448798950512827587`\), 0.449248388682410038`}, {
\(-0.505066313065112826`\), 0.428571428571428381`}, {
\(-0.802332238162223276`\), 0.214285714285714101`}, {
\(-0.85081679346112633`\), \(-1.66533453693773481`*^-16\)}, {
\(-0.802332238162222921`\), \(-0.214285714285714457`\)}, {
\(-0.505066313065112026`\), \(-0.428571428571428736`\)}, {
\(-0.448798950512827587`\), \(-0.449248388682410482`\)}}]}}\)

The pairs here are {x,y} pairs in 2 space which lie on the contour lines. The idea is to convert the pair into {x, y, z} coordinates that will lie on the surface the plot above. In this case, each {x, y} pair should be converted to a {x, y, y^2 - Cos[x]} triple. Mathematica allows this type of conversion using Replacement Rules. Here is an example using the first pair in listOlines[[1]]:

In[39]:= {3.14159, 1.39574} /. {x_, y_} -> {x, y, y^2 - Cos[x]}
Out[39]= {3.14159,1.39574,2.94809}

This can be done wholesale with listOlines[[1]] since the only {x,y} pairs in the list are points on the contour lines. If this were not the case we would have to do some more processing of listOlines[[1]].

In fact the transformed list can be plotted directly. Note that I add an offset factor of .01 to the z coordinate. This will allow the lines to appear to be ''on top'' of the surface. The offset factor will depend on the range of your graph.

In[40]:= ctrlines3D = Show[Graphics3D[listOlines[[1]]/. {x_, y_} -> {x, y, y^2 - Cos[x]}]
]
Out[40]= -Graphics3D-

Now you can show the original surface and the ''contour lines'' together.

In[41]:= Show[gr, ctrlines3D]
Out[41]= -Graphics3D-

Tom Zeller
Forum Moderator

URL: ,

Subject (listing for 'overlay of a surface graph and a contourn graph')
Author Date Posted
overlay of a surface graph and a contourn graph Mauricio Mar... 09/28/98 3:03pm
Re: overlay of a surface graph and a contourn g... Forum Modera... 09/29/98 3:20pm
< Previous CommentHelp | Reply To Comment | Reply To Topic