title: Matplotlib radar chart
body: <p>so I was experimenting with <a href="http://matplotlib.org/examples/api/radar_chart.html" rel="nofollow">this</a> matplotlib example. In the data part I tried to make the graphic from a tuple like this:</p>
  
  <pre><code>data =    data[0:8]
  
  f1_CO =   [0.88, 0.02, 0.02, 0.02, 0.00, 0.05, 0.00, 0.05, 0.00] 
  f1_O3 =   [0.89, 0.01, 0.07, 0.00, 0.00, 0.05, 0.00, 0.00, 0.03] 
  f1_both = [0.86, 0.01, 0.08, 0.00, 0.00, 0.04, 0.00, 0.00, 0.01] 
  </code></pre>
  
  <p>But it go wrong because it says:</p>
  
  <pre><code>ValueError: x and y must have same first dimension
  </code></pre>
  
  <p>So, what do I have to change to make it possible to make a plot from a tuple?
  The complete code is :</p>
  
  <pre><code>from matplotlib.projections.polar import PolarAxes 
  from matplotlib.projections import register_projection 
  from pylab import *
  import wave
  import struct
  
  def radar_factory(num_vars, frame='polygon'): 
       """Create a radar chart with `num_vars` axes. 
       """ 
       # calculate evenly-spaced axis angles 
       theta = 2*pi * linspace(0, 1-1/int(num_vars), num_vars) 
       #print theta
       #print
       # rotate theta such that the first axis is at the top 
       theta += pi/2 
  
       def draw_poly_frame(self, x0, y0, r): 
           # TODO: should use transforms to convert (x, y) to (r, theta) 
           verts = [(r*cos(t) + x0, r*sin(t) + y0) for t in theta] 
           return Polygon(verts, closed=True) 
  
       def draw_circle_frame(self, x0, y0, r): 
           return Circle((x0, y0), r) 
  
       frame_dict = {'polygon': draw_poly_frame, 'circle': draw_circle_frame} 
       if frame not in frame_dict: 
           raise ValueError, 'unknown value for `frame`: %s' % frame 
  
       class RadarAxes(PolarAxes): 
           """Class for creating a radar chart (a.k.a. a spider or star chart) 
  
           http://en.wikipedia.org/wiki/Radar_chart 
           """ 
           name = 'radar' 
           # use 1 line segment to connect specified points 
           RESOLUTION = 1
           # define draw_frame method 
           draw_frame = frame_dict[frame] 
  
           def fill(self, *args, **kwargs): 
               """Override fill so that line is closed by default""" 
               closed = kwargs.pop('closed', True) 
               return super(RadarAxes, self).fill(closed=closed, *args,**kwargs) 
  
           def plot(self, *args, **kwargs): 
               """Override plot so that line is closed by default""" 
               lines = super(RadarAxes, self).plot(*args, **kwargs) 
               for line in lines: 
                   self._close_line(line) 
  
           def _close_line(self, line): 
               x, y = line.get_data() 
               # FIXME: markers at x[0], y[0] get doubled-up 
               if x[0] != x[-1]: 
                   x = concatenate((x, [x[0]])) 
                   y = concatenate((y, [y[0]])) 
                   line.set_data(x, y) 
  
           def set_varlabels(self, labels, rvals, rlabels): 
               self.set_thetagrids(theta * 180/pi, labels) 
               self.set_rgrids(rvals, labels=rlabels, size='small')
  
           def get_axes_patch(self): 
               x0, y0 = (0.5, 0.5) 
               r = 0.5 
               return self.draw_frame(x0, y0, r)
  
       register_projection(RadarAxes) 
       return theta 
  
  
  if __name__ == '__main__':
  
  
      w = wave.open('C:/Python27/demo1.wav','r')
      nf = w.getnframes()
      sw = w.getsampwidth()
      assert(sw==2)
      rf = w.readframes(nf)
      w.close()
      data = struct.unpack("%sh" %nf,rf)
      for i in range(9):
          print i,data[i]
  
      N = 9
      theta = radar_factory(N) 
  
      data =    data[0:8]
      f1_CO =   [0.88, 0.02, 0.02, 0.02, 0.00, 0.05, 0.00, 0.05, 0.00] 
      f1_O3 =   [0.89, 0.01, 0.07, 0.00, 0.00, 0.05, 0.00, 0.00, 0.03] 
      f1_both = [0.86, 0.01, 0.08, 0.00, 0.00, 0.04, 0.00, 0.00, 0.01] 
  
      f2_base = [0.07, 0.95, 0.04, 0.05, 0.00, 0.02, 0.01, 0.00, 0.00]
      f2_CO =   [0.08, 0.94, 0.04, 0.02, 0.00, 0.01, 0.12, 0.04, 0.00] 
      f2_O3 =   [0.07, 0.95, 0.05, 0.04, 0.00, 0.02, 0.12, 0.00, 0.00] 
      f2_both = [0.09, 0.95, 0.02, 0.03, 0.00, 0.01, 0.13, 0.06, 0.00] 
  
      f3_base = [0.01, 0.02, 0.85, 0.19, 0.05, 0.10, 0.00, 0.00, 0.00]
      f3_CO =   [0.01, 0.01, 0.79, 0.10, 0.00, 0.05, 0.00, 0.31, 0.00] 
      f3_O3 =   [0.01, 0.02, 0.86, 0.27, 0.16, 0.19, 0.00, 0.00, 0.00] 
      f3_both = [0.01, 0.02, 0.71, 0.24, 0.13, 0.16, 0.00, 0.50, 0.00] 
  
      f4_base = [0.01, 0.01, 0.02, 0.71, 0.74, 0.70, 0.00, 0.00, 0.00]
      f4_CO =   [0.00, 0.02, 0.03, 0.38, 0.31, 0.31, 0.00, 0.59, 0.00] 
      f4_O3 =   [0.01, 0.03, 0.00, 0.32, 0.29, 0.27, 0.00, 0.00, 0.95] 
      f4_both = [0.01, 0.03, 0.00, 0.28, 0.24, 0.23, 0.00, 0.44, 0.88] 
  
      f5_base = [0.02, 0.01, 0.07, 0.01, 0.21, 0.12, 0.98, 0.00, 0.00]
      f5_CO =   [0.02, 0.02, 0.11, 0.47, 0.69, 0.58, 0.88, 0.00, 0.00] 
      f5_O3 =   [0.02, 0.00, 0.03, 0.37, 0.56, 0.47, 0.87, 0.00, 0.00] 
      f5_both = [0.02, 0.00, 0.18, 0.45, 0.64, 0.55, 0.86, 0.00, 0.16] 
  
      fig = figure(figsize=(9,9))
      fig.subplots_adjust(wspace=0.25, hspace=0.20)
      axlist = []
      axisNum = 0
      bases = [data, f2_base, f3_base, f5_base, f4_base]
      COs = [f1_CO, f2_CO, f3_CO, f4_CO, f5_CO]
      O3s = [f1_O3, f2_O3, f3_O3, f4_O3, f5_O3]
      boths = [f1_both, f2_both, f3_both, f4_both, f5_both]
      everything = [bases, COs, O3s, boths]
      titles = ['Muestreo 1', 'Muestreo 2', 'Muestreo 3', 'Muestreo 4']
      colors = ['b', 'r', 'g', 'm', 'y']
      for row in range(2):
          for col in range(2):
              axisNum += 1
              if axisNum == 2:
                   #Unfortunately, it looks like the loc keyword to legend() is 
                   #relative to a specific subplot, rather than the figure itself. 
                   #So, the positioning seen looks good, but if you resize the 
                   #figure to be larger the legend becomes obviously bound to a 
                   #specific subplot. This is in contrast to how the position works
                   #in something like figtext(). Had trouble using figlegend(), but
                   #need to try some more...
                  legend(('Factor 1', 'Factor 2', 'Factor 3', 'Factor 4', 
                           'Factor 5'), loc=(0.95, 0.895), borderpad=0.01, 
                           shadow=False, prop=matplotlib.font_manager
                           .FontProperties(size='smaller'), markerscale=0.4)
  
              data = everything[axisNum-1]
              ax = fig.add_subplot(2, 2, axisNum, projection='radar')
              ax.set_title(titles[axisNum-1], weight='bold', size='medium', 
                            horizontalalignment='center', 
                            verticalalignment='center', 
                            position=(0.5, 1.1))
              p1 = ax.plot(theta, data[0], color=colors[0]) 
              p2 = ax.plot(theta, data[1], color=colors[1])
              p3 = ax.plot(theta, data[2], color=colors[2])
              p4 = ax.plot(theta, data[3], color=colors[3])
              p5 = ax.plot(theta, data[4], color=colors[4])
              ax.fill(theta, data[0], facecolor=colors[0])  
              ax.fill(theta, data[1], facecolor=colors[1]) 
              ax.fill(theta, data[2], facecolor=colors[2])     
              ax.fill(theta, data[3], facecolor=colors[3]) 
              ax.fill(theta, data[4], facecolor=colors[4]) 
               #axlist.extend(ax) #This does not work because ax is a 
                                  #RadarAxesSubplot object, which is not iterable
              axlist.append(ax)  #append() works because it simply tacks on to 
                                  #the list, as opposed to merging items from two
                                  #lists
              for patch in ax.patches: 
                  patch.set_alpha(0.25) 
  
  
      figtext(0.5, 0.965,  '5-Factor Solution Profiles Across Four Scenarios', ha='center', color='black', weight='bold', size='large')
  
  
  
       #Crudely plot the grid lines I want to see: normalized concentrations of
       #chemicals range from 0 to 1...
      radiiGrid = [0.2, 0.4, 0.6, 0.8]
      theta_rgrid = radar_factory(100)
      for ax in axlist:
          for r in radiiGrid:
              radii = repeat(r, 100)
              ax.plot(theta_rgrid, radii, color='lightgrey')
  
       # FIXME: legend doesn't work when fill is called 
      spokeLabels = ['Sulfate', 'Nitrate', 'EC', 'OC1', 'OC2', 'OC3', 'OP',
   'CO', 
                      'O3']
      radiiLabels = [str(rg) for rg in radiiGrid]
      for ax in axlist:
          ax.set_varlabels(spokeLabels, radiiGrid, radiiLabels)
  
      show()
  </code></pre>