Shade 'cells' in polar plot with matplotlib

前端 未结 3 981
南方客
南方客 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:04

    Sure! Just use pcolormesh on a polar axes.

    E.g.

    import matplotlib.pyplot as plt
    import numpy as np
    
    # Generate some data...
    # Note that all of these are _2D_ arrays, so that we can use meshgrid
    # You'll need to "grid" your data to use pcolormesh if it's un-ordered points
    theta, r = np.mgrid[0:2*np.pi:20j, 0:1:10j]
    z = np.random.random(theta.size).reshape(theta.shape)
    
    
    fig, (ax1, ax2) = plt.subplots(ncols=2, subplot_kw=dict(projection='polar'))
    
    
    ax1.scatter(theta.flatten(), r.flatten(), c=z.flatten())
    ax1.set_title('Scattered Points')
    
    ax2.pcolormesh(theta, r, z)
    ax2.set_title('Cells')
    
    for ax in [ax1, ax2]:
        ax.set_ylim([0, 1])
        ax.set_yticklabels([])
    
    plt.show()
    

    enter image description here

    If your data isn't already on a regular grid, then you'll need to grid it to use pcolormesh.

    It looks like it's on a regular grid from your plot, though. In that case, gridding it is quite simple. If it's already ordered, it may be as simple as calling reshape. Otherwise, a simple loop or exploiting numpy.histogram2d with your z values as weights will do what you need.

    0 讨论(0)
  • 2020-12-28 22:22

    This can be done quite nicely by treating it as a polar stacked barchart:

    import matplotlib.pyplot as plt
    import numpy as np
    from random import choice
    
    fig = plt.figure()
    ax = fig.add_axes([0.1, 0.1, 0.8, 0.8], polar=True)
    
    for i in xrange(12*8):
        color = choice(['navy','maroon','lightgreen'])
        ax.bar(i * 2 * np.pi / 12, 1, width=2 * np.pi / 12, bottom=i / 12,
               color=color, edgecolor = color)
    plt.ylim(0,10)
    ax.set_yticks([])
    plt.show()
    

    Produces:

    enter image description here

    0 讨论(0)
  • 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()
    
    0 讨论(0)
提交回复
热议问题