# How to plot polar contourplot in julia

**URL:** <https://discourse.julialang.org/t/how-to-plot-polar-contourplot-in-julia/73198>\
**Category:** New to Julia\
**Tags:** plotting, pyplot, plots, gr\
**Created:** [December 16, 2021, 11:59am UTC](https://discourse.julialang.org/t/how-to-plot-polar-contourplot-in-julia/73198 "2021-12-16T11:59:42Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![usman0311](https://avatars.discourse-cdn.com/v4/letter/u/2bfe46/32.png) [@usman0311](https://discourse.julialang.org/u/usman0311)\
**Post date:** [December 16, 2021, 11:59am UTC](https://discourse.julialang.org/t/how-to-plot-polar-contourplot-in-julia/73198/1 "2021-12-16T11:59:42Z")

</div>

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

 ![pole122](https://global.discourse-cdn.com/julialang/original/3X/3/4/34f767cf552a5f0ffc4969925fd982618fad2e10.png)  
I m using the following script:

```julia
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")

```

---

<div class="post-metadata">

**Author:** ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)\
**Post date:** [December 16, 2021, 12:06pm UTC](https://discourse.julialang.org/t/how-to-plot-polar-contourplot-in-julia/73198/2 "2021-12-16T12:06:20Z")

</div>

> [@How to plot heatmap and contour plots in polar coordinates with Plots.jl?](https://discourse.julialang.org/t/how-to-plot-heatmap-and-contour-plots-in-polar-coordinates-with-plots-jl/7817):
>
> It is really nice to have polar coordinates implemented in Plots.jl: using Plots; gr() plot(linspace(0,3π,100), rand(100), proj=:polar, m=:red) [polar] and even nicer to interact with them using the PlotlyJS backend. However, I can’t find documentation on how to plot heatmaps and contour plots in polar coordinates. Could you please give a hand?

Maybe you want the option `proj = :polar`.

---

<div class="post-metadata">

**Author:** ![usman0311](https://avatars.discourse-cdn.com/v4/letter/u/2bfe46/32.png) [@usman0311](https://discourse.julialang.org/u/usman0311)\
**Post date:** [December 16, 2021, 12:56pm UTC](https://discourse.julialang.org/t/how-to-plot-polar-contourplot-in-julia/73198/3 "2021-12-16T12:56:40Z")

</div>

Its not working, I am getting following error:

```julia
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

```

---

<div class="post-metadata">

**Author:** ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)\
**Post date:** [December 16, 2021, 1:04pm UTC](https://discourse.julialang.org/t/how-to-plot-polar-contourplot-in-julia/73198/4 "2021-12-16T13:04:47Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![usman0311](https://avatars.discourse-cdn.com/v4/letter/u/2bfe46/32.png) [@usman0311](https://discourse.julialang.org/u/usman0311)\
**Post date:** [December 16, 2021, 1:06pm UTC](https://discourse.julialang.org/t/how-to-plot-polar-contourplot-in-julia/73198/5 "2021-12-16T13:06:38Z")

</div>

I am using the following code (Julia 1.7.0).

```julia
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

---

<div class="post-metadata">

**Author:** ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)\
**Post date:** [December 16, 2021, 1:17pm UTC](https://discourse.julialang.org/t/how-to-plot-polar-contourplot-in-julia/73198/6 "2021-12-16T13:17:24Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [December 16, 2021, 3:45pm UTC](https://discourse.julialang.org/t/how-to-plot-polar-contourplot-in-julia/73198/8 "2021-12-16T15:45:39Z")

</div>

Plots.jl pyplot() backend works (_[see example here](https://discourse.julialang.org/t/how-to-plot-heatmap-and-contour-plots-in-polar-coordinates-with-plots-jl/7817/2)_) but with some limitations and PyPlot.jl seems to work best (_[see github post here](https://github.com/JuliaPlots/Plots.jl/issues/1007)_).

PyPlot example below:

```julia
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](https://global.discourse-cdn.com/julialang/original/3X/b/c/bc4f2938f2bc01378f5ea596a0e89d145f55366c.png)

---

<div class="post-metadata">

**Author:** ![usman0311](https://avatars.discourse-cdn.com/v4/letter/u/2bfe46/32.png) [@usman0311](https://discourse.julialang.org/u/usman0311)\
**Post date:** [December 16, 2021, 5:39pm UTC](https://discourse.julialang.org/t/how-to-plot-polar-contourplot-in-julia/73198/9 "2021-12-16T17:39:59Z")

</div>

Thanks a lot, Rafael it works

---

<div class="post-metadata">

**Author:** ![jheinen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jheinen/32/2787_2.png) [@jheinen](https://discourse.julialang.org/u/jheinen)\
**Post date:** [December 18, 2021, 1:25am UTC](https://discourse.julialang.org/t/how-to-plot-polar-contourplot-in-julia/73198/10 "2021-12-18T01:25:53Z")

</div>

Also works in GR:

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

```

 ![Screen Shot 2021-12-18 at 02.24.53](https://global.discourse-cdn.com/julialang/original/3X/2/f/2f94e2e42c2422f5086fda8443de98a75f73c863.png)

---

<div class="post-metadata">

**Author:** ![usman0311](https://avatars.discourse-cdn.com/v4/letter/u/2bfe46/32.png) [@usman0311](https://discourse.julialang.org/u/usman0311)\
**Post date:** [December 18, 2021, 4:24am UTC](https://discourse.julialang.org/t/how-to-plot-polar-contourplot-in-julia/73198/11 "2021-12-18T04:24:07Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [December 18, 2021, 6:02am UTC](https://discourse.julialang.org/t/how-to-plot-polar-contourplot-in-julia/73198/12 "2021-12-18T06:02:02Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jheinen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jheinen/32/2787_2.png) [@jheinen](https://discourse.julialang.org/u/jheinen)\
**Post date:** [December 18, 2021, 7:30am UTC](https://discourse.julialang.org/t/how-to-plot-polar-contourplot-in-julia/73198/13 "2021-12-18T07:30:51Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [December 18, 2021, 8:13am UTC](https://discourse.julialang.org/t/how-to-plot-polar-contourplot-in-julia/73198/14 "2021-12-18T08:13:12Z")

</div>

> [@jheinen](#):
>
> `polarheatmap(funp.(ρ,θ'))`

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.

---

<div class="post-metadata">

**Author:** ![jheinen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jheinen/32/2787_2.png) [@jheinen](https://discourse.julialang.org/u/jheinen)\
**Post date:** [December 18, 2021, 9:10am UTC](https://discourse.julialang.org/t/how-to-plot-polar-contourplot-in-julia/73198/15 "2021-12-18T09:10:49Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jheinen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jheinen/32/2787_2.png) [@jheinen](https://discourse.julialang.org/u/jheinen)\
**Post date:** [February 11, 2022, 11:31pm UTC](https://discourse.julialang.org/t/how-to-plot-polar-contourplot-in-julia/73198/16 "2022-02-11T23:31:26Z")

</div>

I think that’s fixed in the meantime:

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

```

 ![Screenshot 2022-02-12 at 00.29.41](https://global.discourse-cdn.com/julialang/original/3X/f/9/f902a2aa054e11d57d5eb74fddb1aba286627c20.png)
