Plot line segments (e.g. n-th roots on unit circle ) in polar projection

Question:

  1. How to plot n-th roots on the unit circle in polar projection in Julia? Is this the intended/natural way to do it, or is there any better/lighter-weight way than the following?

  2. How to plot “π/6” instead of the value of θ in plot title and in annotations?

# Plot line segment (e.g. n-th roots on unit circle) in polar prejection
using Plots; gr()

# set step size θ
θ = π/6

# plot points at angle θ at step size π/6 from 0 to 2π, points on the circle NOT to be beconnected, a way to do that is
# plot one root at a time as a segment with first end point at orign [0,0], and second end point at [i*π/6,1].

b1=Float64[1:2;].-1.0

# Q(3): how to plot "π/6" instead of the value of θ in plot title and in annotations?
plt = plot(proj=:polar, m=0, title="polar projection step size $θ")
for i in 1:Int64(2π/θ)
    # with marker point size m=5
    plot!(plt, 0:i*θ:i*θ, b1, m=5, annotations=(b1[2]*cos(i*θ), b1[2]*sin(i*θ), text("$i π/6", :left)))
end

# return the plot object, won't display without this
plt

# plotted graph is attached below

I’ve edited your post to make it considerably less shouty.

1 Like

Thank you very much for letting me know this. I was wondering how to do it, but wasn’t patient enough to figure out.

The for loop returns nothing. You need to return the plot object.

Another way is to start off with p = plot()
and then put

p

as the last line in order to return that object.

Thank you very much. Worked with your suggestion. Tried the code with plot object plt in the plot! and without, it didn’t change the outcome - I figured it was because plot! made the system to look for the most recent plot object to modify on.

plt = plot(proj=:polar, m=0, title="polar projection step size $θ")
for i in 1:Int64(2π/θ)
    # with marker point size m=5
    plot!(plt, 0:i*π/6:i*π/6, b1, m=5, annotations=(b1[2]*cos(i*π/6), b1[2]*sin(i*π/6), text("$i π/6", :left)))
end
plt
1 Like