A normal PolarPlot takes in a function of an angle $\theta$ and outputs a 2D graph in which the value of the function at each value of $\theta$ is encoded by the graph's radial distance from the origin at that angle. For example, for the polar function $f(\theta)=sin(3\theta)$, PolarPlot[Sin[3θ], {θ, 0, 2Pi}] looks like this:

Polar plot of f(x)=sin(3x)

I'd like to create a similar plot, but instead of indicating the value of the function by means of the radial distance from the origin, I'd like to instead use the color of the point. For simplicity, assume the function I wish to plot is confined to values between -1 and 1. Using the same example as above, my desired representation of $f(x)=sin(3\theta)$ would be something like this:

Polar plot of f(x)=1 with points colored per color(x)=sin(3x)

I will post the code I used to create this image below as an answer, but I'm guessing I can do better. I'm not committed to the output being a plot; just a circle, colored appropriately, would be sufficient.


2 Answers 2

ParametricPlot[{Cos[t], Sin[t]}, {t, 0, 2 π}, 
 ColorFunction -> Function[{x, y, t}, Blend[{Blue, Red}, Sin[3 t]]], 
 ColorFunctionScaling -> False]

enter image description here


My current solution, which I am not thrilled with:

I compute the value of the polar function f at 1024 values of θ, then generate a ListPolarPlot with 1024 separate datasets. Each dataset consists of a single point {θ, 1} and each dataset is colored via PlotStyle with a color determined by f[θ].

colorPolarPlot[f_] := With[{
  colorfunc = RGBColor[(1+#)/2, 0, (1-#)/2] &,
  data = Transpose @ Table[{{θ, 1}, f[θ]}, {θ, 0, 2Pi, 2Pi/1024}]
 }, Legended[
  ListPolarPlot[List /@ data〚1〛, PlotStyle -> colorfunc /@ data〚2〛],
  BarLegend[{colorfunc, {-1, 1}}]

The image in the question is produced by

colorPolarPlot @ Function[θ, Sin[3θ]]

Your Answer

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

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