Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shade 'cells' in polar plot with matplotlib

I've got a bunch of regularly distributed points (θ = n*π/6, r=1...8), each having a value in [0, 1]. I can plot them with their values in matplotlib using

polar(thetas, rs, c=values)

But rather then having just a meagre little dot I'd like to shade the corresponding 'cell' (ie. everything until halfway to the adjacent points) with the colour corresponding to the point's value:

Polar plot with shaded cells

(Note that here my values are just [0, .5, 1], in really they will be everything between 0 and 1. Is there any straight-forward way of realising this (or something close enough) with matplotlib? Maybe it's easier to think about it as a 2D-histogram?

like image 829
Manuel Ebert Avatar asked May 31 '12 16:05

Manuel Ebert


1 Answers

Well, it's fairly unpolished overall, but here's a version that rounds out the sections.

from matplotlib.pylab import *
ax = subplot(111, projection='polar')

# starts grid and colors
th = array([pi/6 * n for n in range(13)]) # so n = 0..12, allowing for full wrapping
r = array(range(9)) # r = 0..8
c = array([[random_integers(0, 10)/10 for y in range(th.size)] for x in range(r.size)])

# The smoothing
TH = cbook.simple_linear_interpolation(th, 10)

# Properly padding out C so the colors go with the right sectors (can't remember the proper word for such segments of wedges)
# A much more elegant version could probably be created using stuff from itertools or functools
C = zeros((r.size, TH.size))
oldfill = 0
TH_ = TH.tolist()

for i in range(th.size):
    fillto = TH_.index(th[i])

    for j, x in enumerate(c[:,i]):
        C[j, oldfill:fillto].fill(x)

    oldfill = fillto

# The plotting
th, r = meshgrid(TH, r)
ax.pcolormesh(th, r, C)
show()
like image 103
JAB Avatar answered Sep 19 '22 11:09

JAB