Matplotlib density plot in polar coordinates?

6.6k views Asked by At

I have an array saved as a txt file that has entries corresponding to the value of a distribution in polar coordinates. So it looks like this:

  f(r1,theta1) f(r1, theta2) ..... f(r1, theta_max)
  f(r2,theta1) f(r2, theta2) .....        .
        .                                 .
        .                                 .
        .                                 .
  f(r_max,theta1) .................f(r_max, theta_max)

I want to do a density plot of f (the higher f is, the more red I want the color to be). Is there a way to do this with matplotlib? Explicit code would be helpful, as I am majorly new to this.

1

There are 1 answers

0
CT Zhu On

In this example, a is you theta1...thetan, b is your r1...rn, c is your f(a, b):

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.cm as cm

#fake data:
a = np.linspace(0,2*np.pi,50)
b = np.linspace(0,1,50)
A, B = np.meshgrid(a, b)
c = np.random.random(A.shape)

#actual plotting
ax = plt.subplot(111, polar=True)
ax.set_yticklabels([])
ctf = ax.contourf(a, b, c, cmap=cm.jet)
plt.colorbar(ctf)

enter image description here

Essentially a filled contour plot in polar axis. You can specify alternative colormap with the cmap=.... cm.jet goes from blue to red, with red being the largest value.