title: Two dimensional histogram in polar coordinates with variable number of bins using Matplotlib
body: <p>I am trying to create a two-dimensional colored histogram in polar coordinates using Matplotlib.</p>
  
  <p>The bin edges will be spaced evenly along the radial axis, but there will be an arbitrary number of bins along the angle axis at a given radius.
  I am attempting this because I want the size of the bins to be relatively constant as the radius increases.
  So at a small radius, there will be only a few angle bins, but at a larger radius, there will be more.</p>
  
  <p>Here is my first attempt (although it seems a bit convoluted to me) using pcolormesh.
  As you can see, there is white areas around the bins edges because the mesh lines remain straight even in polar coordinates. (Hmm, first post for me so I cannot attach pictures.)</p>
  
  <p>Here is the <a href="https://dl.dropboxusercontent.com/u/53993075/test.png" rel="nofollow">link</a>.</p>
  
  <p>Is there a way I can make the white areas disappear (preferably by making concentric circular bin edges)?</p>
  
  <p><pre>import matplotlib.pyplot as plt
  import numpy as np</p>
  
  <code># bin properties in radial coordinate
  nBins_r = 2 # number of bins in radial direction
  r_max = 1.0
  r_min = 0.0
  delta_r = (r_max - r_min)/float(nBins_r) # size of increment in r
  
  # bin properties in angular (theta) coordinate
  nBins_theta_list = [] # number of bins depends on radius
  delta_circumf = delta_r # set bin edge length same in both coordinates
  circumf_max = 2.0*np.pi*r_max
  nBins_theta_max = int(circumf_max/delta_circumf) # max number of bins in theta needed
  
  # fill radial bin edges
  binEdges_r = [0.0]
  for i in xrange(nBins_r):
      binEdges_r.append(float(i+1)*delta_r)
      binEdges_r.append(float(i+1)*delta_r) # do twice for next bin in r
  binEdges_r.pop() # last entry does not need to be repeated
  
  # find size of array needed for angular bin edges, fill later
  binEdges_theta = np.zeros(nBins_theta_max+1)
  
  # create meshgrid
  thetas, rs = np.meshgrid(binEdges_theta, binEdges_r, indexing='xy')
  
  # fill matrix of angular bin edges
  for i_r in xrange(nBins_r): # iterate through different r's
      r_edge = float(i_r+1)*delta_r # current radius
      circumf = 2.0*np.pi**r_edge # current circumference at radius r
      nBins_theta = int(circumf/delta_circumf) # number of bins in theta
      delta_theta = 2.0*np.pi/nBins_theta
      for i_theta in xrange(nBins_theta+1):
          thetas[2*i_r, i_theta] = i_theta*delta_theta - 0.5*delta_theta
          thetas[2*i_r+1, i_theta] = i_theta*delta_theta - 0.5*delta_theta # do twice for next bin in r
      nBins_theta_list.append(nBins_theta)
  
  # weights of bins to be colored
  w = np.full((2*nBins_r, nBins_theta_max), -1)
  
  temp = 0.0
  for i in xrange(nBins_r):
      for j in xrange(nBins_theta_list[i]):
          temp += 1
          w[2*i, j] = temp # set some arbitrary weight
  ws = np.ma.masked_values(w, -1)
  
  # create plot
  ax = plt.subplot(projection="polar")
  ax.pcolormesh(thetas, rs, ws)
  plt.show()
  </code></pre>
  
  <p></p>