Shade 'cells' in polar plot with matplotlib

前端 未结 3 980
南方客
南方客 2020-12-28 21:25

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

         


        
3条回答
  •  有刺的猬
    2020-12-28 22:28

    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()
    

提交回复
热议问题