How to plot polar contourplot in julia

In material science, we frequently have to plot pole figures to know the symmetry of the as-grown thin film.
The Pole figure is a polar contour plot. I struggle to plot it in Julia. It would be a great help if someone helped me out.
The pole figure should look like:

But what I am getting is like


I m using the following script:

using DataFrames, Plots, DelimitedFiles, Glob, LaTeXStrings

f=Glob.glob("*.txt")

df=readdlm(f[1],comment_char='#',comments=true)
df=DataFrame(df,:auto)

Chi,phi,I=df[:,1],df[:,2],df[:,3]

plotlyjs()

c=contour(Chi,phi,I,fill=true,c=:Spectral,frame=:box,axis=:polar,xlabel=L"$\chi$",ylabel=L"$\phi$", width=1000, height=1000)
savefig(c,"pole122.png")

Maybe you want the option proj = :polar.

Its not working, I am getting following error:

MethodError: no method matching heatmap_edges(::Vector{Float64}, ::Symbol, ::Vector{Float64}, ::Symbol, ::Tuple{Int64})
Closest candidates are:
  heatmap_edges(::AbstractVector, ::Symbol, ::AbstractVector, ::Symbol, ::Tuple{Int64, Int64}) at C:\Users\usmaa\.julia\packages\Plots\FCM0H\src\utils.jl:304
  heatmap_edges(::AbstractVector, ::Symbol, ::AbstractVector, ::Symbol, ::Tuple{Int64, Int64}, ::Bool) at C:\Users\usmaa\.julia\packages\Plots\FCM0H\src\utils.jl:304
  heatmap_edges(::AbstractVector, ::Symbol) at C:\Users\usmaa\.julia\packages\Plots\FCM0H\src\utils.jl:294

Please share the exact code you tried to run and quote it in backquotes so that Discourse highlights it correctly.

I am using the following code (Julia 1.7.0).

using DataFrames, Plots, DelimitedFiles, Glob, LaTeXStrings

f=Glob.glob("*.txt")

df=readdlm(f[1],comment_char='#',comments=true)
df=DataFrame(df,:auto)

Chi,phi,I=df[:,1],df[:,2],df[:,3]

pyplot()

c=heatmap(Chi,phi,I,fill=true,c=:Spectral,frame=:box,proj=:polar,xlabel=L"$\chi$",ylabel=L"$\phi$", width=1000, height=1000)
savefig(c,"pole122.png")


It uses my data file , how do I share it

Ideally you don’t need to share a file, just create fake data and make a minimum working example. That way we can easily help without downloading data.

Plots.jl pyplot() backend works (see example here) but with some limitations and PyPlot.jl seems to work best (see github post here).

PyPlot example below:

using PyPlot
fig = plt.figure()
ax = fig.add_subplot(111, projection="polar")
ρ = LinRange(0., 7, 200)
θ = LinRange(0., 2π, 360)
funp(ρ,θ) =  sin(2ρ) * cos(θ)
pc = pcolormesh(θ, ρ, funp.(ρ,θ'))
cbar = plt.colorbar(pc)
cbar.set_label("Intensity")
ax[:grid](true)

PyPlot_polar_heatmap

3 Likes

Thanks a lot, Rafael it works

Also works in GR:

using GR
ρ = LinRange(0., 7, 200)
θ = LinRange(0., 2π, 360)
funp(ρ,θ) =  sin(2ρ) * cos(θ)
polarheatmap(funp.(ρ,θ'))

4 Likes

Thanks, Jheinen. This solution also works, but how could one change other plot properties like color and levels

@jheinen, what do the labels 100.5 and 0.5 mean?

Those labels are derived from the ρ range. It’s a bug :disappointed_relieved:

Don’t you have to specify the polar coordinates here in order to get the correct ranges? See above how the similar PyPlot function was called.

Thanks for the hint. I will change the heatmap functions next week.

2 Likes

I think that’s fixed in the meantime:

using GR
ρ = LinRange(0., 7, 200)
θ = LinRange(0., 2π, 360)
funp(ρ,θ) =  sin(2ρ) * cos(θ)
polarheatmap(ρ,θ,funp.(ρ,θ')')

3 Likes