I would like to know whether it is possible to change part of the Style of a line in a 3D plot, when the line is under a surface, so that it seems obstructed by the surface. Of course I can always work this out using some elementary geometry and ViewPoint, but I want to know wether I can make this automatic.

Just to make things more clear, here is an example

graph1 = Show[
   Plot3D[{0, 1}, {x, 0, 1}, {y, 0, 1}, BoxRatios -> 1, 
    PlotStyle -> {{Opacity[0.3], Red}, {Opacity[0.3], Blue}}, 
    Mesh -> False], 
   Graphics3D[{{Thick, Line[{{1/2, 1/2, 0}, {1/2, 1/2, 1}}]}}]];

I would like to make, in the example above, the bit of the line that is seen under the plane z=1 to appear as a dashed line, but the remainder of the line should appear as a solid line.

I tried


but it did not work.

  • $\begingroup$ The line is always smaller 1 in your plot!? $\endgroup$ Jan 28, 2021 at 14:16
  • $\begingroup$ Perhaps I did don't explain well. The line will be hidden by the plane z=1 and I want the hidden part to appear as dashed. This will of course depend on the ViewPoint we set in the options in Plot3D. $\endgroup$
    – user12588
    Jan 28, 2021 at 20:41

1 Answer 1


Here is a solution using MeshFunction and MeshShading:

graph1 = Show[
Plot3D[{0, 1}, {x, 0, 1}, {y, 0, 1}, BoxRatios -> 1,PlotStyle ->{{Opacity[0.3], Red}, {Opacity[0.3], Blue}},Mesh -> False], 
ParametricPlot3D[  {1/2, 1/2, t} , {t, 0, 2} , PlotStyle -> Thick  
, MeshFunctions -> {#3 &}, Mesh -> {{1}}
, MeshShading -> {Dashed,Automatic}  ]]

enter image description here


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.