3
$\begingroup$

I have this code:

datar = Import["https://pastebin.com/raw/CM7Rj6jC", "Table"];
datar[[All, 1]] = 2 π (datar[[All, 1]])/360;
nlm1 = NonlinearModelFit[datar[[All, {1, 2}]], 
a Cos[t + ϕ]^2, {a, ϕ}, t];
nlm2 = NonlinearModelFit[datar[[All, {1, 3}]], 
a Cos[t + ϕ]^2, {a, ϕ}, t];
plot2 = Show[
ListPolarPlot[{datar[[All, {1, 2}]], datar[[All, {1, 3}]]}, 
PlotStyle -> PointSize[0.02], PolarAxes -> True, 
PolarTicks -> {"Degrees", None}], 
PolarPlot[{nlm1[t], nlm2[t]}, {t, 0, 2 π}]]

Which generates this graph:

enter image description here

I need to set the zero degrees on top and plot clockwise, like so: enter image description here

How can I do that? The tick marks and axes are not important, I just left them for illustration.

$\endgroup$
  • $\begingroup$ do you get what you need if you change the second line in your code to datar[[All, 1]] = 2 \[Pi] (datar[[All, 1]])/360 - 90;? $\endgroup$ – kglr Nov 15 '19 at 23:32
  • $\begingroup$ No, it rotates the data but it remains counterclockwise. $\endgroup$ – Rodrigo Nov 15 '19 at 23:39
3
$\begingroup$

You can rotate your data to make it increase in the clockwise direction by generating it in the following way:

datar = Import["https://pastebin.com/raw/CM7Rj6jC", "Table"];
datar[[All, 1]] = 2 π (datar[[All, 1]])/360;
nlm1 = NonlinearModelFit[datar[[All, {1, 2}]], a Cos[t + ϕ]^2, {a, ϕ}, t];
nlm2 = NonlinearModelFit[datar[[All, {1, 3}]], a Cos[t + ϕ]^2, {a, ϕ}, t];
datar[[All, 1]] = 2 Pi - (# - Pi/2) & /@ datar[[All, 1]];

And plot the polar plot like this:

PolarPlot[
 {nlm1[2 Pi - (t - Pi/2)], nlm2[2 Pi - (t - Pi/2)]},
 {t, 0, 2 π}
 ]

And add the following two options to ListPolarPlot to adjust the ticks and the axis accordingly

PolarTicks -> {Table[{(360 - (deg - 90)) Degree, deg}, {deg, 0, 350, 10}], None},
PolarAxesOrigin -> {{Top, Up}, Automatic},

you will get

Mathematica graphics

PolarAxes will issue a warning, but that warning also appears for examples in the documentation, so it seems like that's a bug that's been introduced at some point. It's best to just ignore it. Quiet can get rid of it altogether.

To check that the formula works, we can take a couple of points to demonstrate. For example, we see that what was previously zero has now become 90 and that what was previously 180 has now become 270. Computing this with the given formula, we get these results, as expected:

Mod[360 - (0 - 90), 360]

90

Mod[360 - (180 - 90), 360]

270

| improve this answer | |
$\endgroup$
  • $\begingroup$ That won't work. Compare the picture with the second one in my question. $\endgroup$ – Rodrigo Nov 15 '19 at 23:54
  • $\begingroup$ @Rodrigo I don't quite understand what it is that you want. I added two simple examples to the post, could you explain what's wrong? $\endgroup$ – C. E. Nov 16 '19 at 0:06
  • $\begingroup$ Ok, look at the second picture of my question. There is a blue dot above the solid blue curve at the position 0 degrees and another one at the position 20 degrees. Those are the actual values from the original picture. I just rotated the origin and changed it to clockwise. Now in your result, those two points are still at the original position, you just moved the tick marks. The expected result is shown in the second picture from my question. $\endgroup$ – Rodrigo Nov 16 '19 at 0:12
  • $\begingroup$ If you look closely, your result is different from what I showed in the second picture. $\endgroup$ – Rodrigo Nov 16 '19 at 0:13
  • $\begingroup$ @Rodrigo ah yes, I didn't see that both sets of points were using the same column in datar so I converted it twice, and on top of that, I converted it before the model fit, so it also affected the fit. What about now? $\endgroup$ – C. E. Nov 16 '19 at 0:35
1
$\begingroup$

You can also transform datar[[All, 1]] before invoking NonlinearModelFit (in the second line of your code block)

datar = Import["https://pastebin.com/raw/CM7Rj6jC", "Table"];
datar[[All, 1]] = 5 Pi /2 - 2 π datar[[All, 1]]/360;
{nlm1, nlm2} = NonlinearModelFit[datar[[All, {1, #}]], a Cos[t + ϕ]^2, {a, ϕ}, t]&/@{2, 3};

If you don't need polar axes and ticks, this is the only change needed:

plt1 = Show[ListPolarPlot[datar[[All, {1, #}]] & /@ {2, 3}, 
    PlotStyle -> PointSize[0.02]], 
 PolarPlot[{nlm1[t], nlm2[t]}, {t, 0, 2 π}], Axes -> False]

enter image description here

If you need to add polar axes and ticks, an alternative apporach is to create an empty SectorChart with the option SectorOrigin -> {{Pi/2, "Clockwise"}, 0} and combine it with your two plots using Show:

angleaxis = SectorChart[{{0, .05 + Max[datar[[All, {2, 3}]]]}}, 
   SectorOrigin -> {{Pi/2, "Clockwise"}, 0}, 
   PolarAxes -> {True, False}, PolarTicks -> {"Degrees", None}];

Show[angleaxis, plt1, ImageSize -> 500]

enter image description here

Alternatively, define a function that constructs the angle axis:

ClearAll[angleAxis]
angleAxis[radius_: 1, angleOrigin_: Automatic, direction_: Automatic, 
  ticks_: Automatic] := SectorChart[{{0, radius}}, 
  SectorOrigin -> {{angleOrigin /. Automatic -> Pi/2, 
     direction /. Automatic -> "Clockwise"}, 0}, 
  PolarAxes -> {True, False}, 
  PolarTicks -> {ticks /. Automatic -> "Degrees", None}]

Examples:

Show[angleAxis[.05 + Max @ datar[[All, {2, 3}]]], plt1, ImageSize -> 500]

enter image description here

Show[angleAxis[.05 + Max @ datar[[All, {2, 3}]], Pi/4, 
  "Counterclockwise", "Radians"], plt1, ImageSize -> 500]

enter image description here

Make multiple angular axes:

SeedRandom[77777]
colors = RandomColor[4];

Graphics[{Thick, FontSize -> 14, MapThread[List, {colors, 
    angleAxis[#, RandomChoice[Subdivide[0, 2 Pi, 12]], 
        RandomChoice[{"Clockwise", "Counterclockwise"}], 
        RandomChoice[{"Degrees", "Radians", 
          Sort@ RandomSample[Most@Subdivide[0, 2 Pi, 12], 6]}]][[1]] & /@ 
    Range[2, 5]}]}, ImageSize -> Large]

enter image description here

| improve this answer | |
$\endgroup$

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.