4
$\begingroup$

I have a contour plot which is a sector of a circle. In the standard form, it is plotted in a framed box (cartesian coordinate). But I want to plot it in a polar coordinate frame. I mean I want to have a grid line in the x-direction and a grid curve for angle part.

Here is my code

ClearAll["Global`*"];
ContourPlot[Cos[x] + Cos[y], {x, 0, 4}, {y, 0, 4}, PlotLegends -> Automatic, 
RegionFunction -> Function[{x, y}, 0.01 <= Sqrt[x^2 + y^2] <= 4 && 0. <=ArcTan[x, y] <= \[Pi]/3],  ImageSize -> Large]

which plots

enter image description here

but I want to have

enter image description here

or

enter image description here

How can I do that?

$\endgroup$
4
$\begingroup$

Update 2: Using OP's ContourPlot with RegionFunction and adding the polar grid lines as Mesh and polar ticks and tick labels as Epilog:

mesh1 = Range[0, Pi/3, Pi/24];
mesh2 = Range[0, Pi/3, Pi/24/5];   
ticks = Join @@ ((First@ Normal@ ParametricPlot[ 4 v { Cos[u], Sin[u]},
  {u, 0, Pi/3}, {v, #, 1}, 
  BoundaryStyle -> None, PlotStyle -> None, 
  MeshFunctions -> {#3 &, #4 &}, Mesh -> {#2, {1}}, 
  MeshStyle -> Directive[Thickness[.005], CapForm["Round"]]]) & @@@ 
     Transpose[{{.95, .975}, {mesh1, mesh2}}]);
labels = Table[Text[Style[j , 14], 
    4 {Cos@j, Sin@j}, {0, -1.2}, {Cos[j - Pi/2], Sin[j - Pi/2]}], {j, mesh1}]; 
ContourPlot[Cos[x] + Cos[y], {x, 0, 4}, {y, 0 , 4}, 
 PlotRange -> {{-1, 5}, {-1, 5}},
 Contours -> 30, ColorFunction -> Hue, 
 PerformanceGoal -> "Quality", PlotPoints -> 50, 
 MeshFunctions -> {ArcTan[#, #2] &, Sqrt[#^2 + #2^2] &  }, 
 Mesh -> {mesh1, 5}, MeshStyle -> White,
 ContourStyle -> None, PlotLegends -> Automatic, 
 RegionFunction -> (0.01 <= Sqrt[#^2 + #2^2] <= 4 && 0. <= ArcTan[#, #2] <= π/3&), 
 Epilog -> {ticks, labels}]

enter image description here

Update: To use the cropped original contour plot as Texture just add the options

TextureCoordinateFunction -> ({#, #2}&), 
TextureCoordinateScaling -> False

to pp. Then Show[pp, Epilog -> {ticks, labels}] gives

enter image description here

Original answer:

Using the method from this answer to use ContourPlot output as Texture in ParametricPlot

cpl = ContourPlot[Cos[x] + Cos[y], {x, -Pi, Pi}, {y, -Pi, Pi}, 
   Contours -> 30, PlotRangePadding -> 0, Frame -> False, 
   ColorFunction -> Hue, PerformanceGoal -> "Quality", PlotPoints -> 50, 
   MaxRecursion -> 3,  ContourStyle -> None, PlotLegends -> Automatic]  ; 

enter image description here

{cp, legend} = {cpl[[1]], RawBoxes@Replace[ToBoxes[cpl[[2, 1]] ], Rule[FrameTicks, _] :>
    Rule[FrameTicks, False], ∞] }
mesh1 = Range[0, Pi/4, Pi/24];
mesh2 = Range[0, Pi/4, Pi/24/5]; 
pp = ParametricPlot[v { Cos[u], Sin[u]}, {u, 0, Pi/4}, {v, .1, 1}, 
    PlotLegends -> legend, ImageSize -> Large, 
    PlotStyle -> Texture[cp], 
    MeshFunctions -> {#3 &, #4 &}, Mesh -> {mesh1, Range[0, 1, .2]}, 
    MeshStyle ->  Directive[White, Thick] , PlotRange -> All, 
    PlotRangeClipping -> False, ImagePadding -> Scaled[.05], 
    ImageSize -> 300, Frame -> False, Axes -> False] /.  Opacity[_] :> Opacity[1] 

enter image description here

You can use ParametricPlot and Text to generate polar ticks and labelsand use them as Epilog in Show:

ticks = Join @@ ((First@ Normal@ParametricPlot[v { Cos[u], Sin[u]}, {u, 0, Pi/4}, 
  {v, #, 1}, BoundaryStyle -> None, PlotStyle -> None, 
 MeshFunctions -> {#3 &, #4 &}, Mesh -> {#2, {1}}, 
 MeshStyle -> Directive[Thickness[.005], CapForm["Round"]]]) & @@@ 
     Transpose[{{.95, .975}, {mesh1, mesh2}}]);
labels = Table[Text[Style[j , 14], {Cos@j, Sin@j}, {0, -1.2}, 
  {Cos[j - Pi/2], Sin[j - Pi/2]}], {j, mesh1}]; 
Show[pp, Epilog -> {ticks, labels}]

enter image description here

Note: see see this answer about the unwanted frame ticks in BarLegend output.

$\endgroup$
  • $\begingroup$ Dear @kglr, why does not your answer crop the contour plot? it scales the plot into the defined region. I wanted to be cropped. Please check the figures which were uploaded in the question $\endgroup$ – AYBRXQD Sep 26 '18 at 7:42
  • $\begingroup$ @Hadi, please see the update. $\endgroup$ – kglr Sep 26 '18 at 7:57
  • $\begingroup$ Thank you, dear @kglr. $\endgroup$ – AYBRXQD Sep 26 '18 at 8:27
  • $\begingroup$ @HAdi, my pleasure. Thank you for the accept. $\endgroup$ – kglr Sep 26 '18 at 8:33
  • 1
    $\begingroup$ @HadiSobhani, fixed now (i had an extra ] at the end of RegionFunction - ...). $\endgroup$ – kglr Sep 26 '18 at 8:37
7
$\begingroup$

Ad hoc solution:

{maj, min} = π/3 FindDivisions[{0, 1}, {12, 4}];

ContourPlot[Cos[x] + Cos[y], {x, y} ∈ Annulus[{0, 0}, {1/100, 4}, {0, π/3}],
            AspectRatio -> Automatic, ColorFunction -> "GrayTones", 
            Epilog -> {Red, Map[Line[Outer[Times, {4 - 1/20, 4}, 
                                           AngleVector[#]]] &, min, {2}],
                       Map[Line[Outer[Times, {4 - 1/10, 4}, AngleVector[#]]] &, maj], 
                       Map[Text[180 # °/π, 4 AngleVector[#], -1.3 AngleVector[#]] &, maj],
                       Circle[{0, 0}, 4, {0, π/3}]}, Frame -> None, 
            PlotLegends -> Automatic, PlotRange -> {{0, 4}, {0, 4}, All}, 
            PlotRangePadding -> Scaled[.05]]

with circular ticks

I'll leave generalization and encapsulation into a routine for somebody else to do.

$\endgroup$
  • $\begingroup$ Dear @J. M. is somewhat okay. I have edited my question. It's kind of you if you make the plot similar to what I added recently. $\endgroup$ – AYBRXQD Sep 25 '18 at 6:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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