title: Adding legend to a radarchart in Python
body: <p>I want to create a radarchart with legends for every plot that is created but until now it has been impossible. Then, you can see the code used to create the plot (thanks to another user that developed it).</p>
  
  <pre><code>import numpy as np
  import matplotlib.pyplot as plt
  import seaborn as sns # improves plot aesthetics
  import pandas as pd
  
  def _invert(x, limits):
      """inverts a value x on a scale from
      limits[0] to limits[1]"""
      return limits[1] - (x - limits[0])
  
  def _scale_data(data, ranges):
      """scales data[1:] to ranges[0],
      inverts if the scale is reversed"""
      for d, (y1, y2) in zip(data[1:], ranges[1:]):
          assert (y1 &lt;= d &lt;= y2) or (y2 &lt;= d &lt;= y1)
      x1, x2 = ranges[0]
      d = data[0]
      if x1 &gt; x2:
          d = _invert(d, (x1, x2))
          x1, x2 = x2, x1
      sdata = [d]
      for d, (y1, y2) in zip(data[1:], ranges[1:]):
          if y1 &gt; y2:
              d = _invert(d, (y1, y2))
              y1, y2 = y2, y1
          sdata.append((d-y1) / (y2-y1) 
                       * (x2 - x1) + x1)
      return sdata
  
  class ComplexRadar():
      def __init__(self, fig, variables, ranges,
                   n_ordinate_levels=6):
          angles = np.arange(0, 360, 360./len(variables))
  
          axes = [fig.add_axes([0.1,0.1,0.9,0.9],polar=True,
                  label = "axes{}".format(i)) 
                  for i in range(len(variables))]
          l, text = axes[0].set_thetagrids(angles, 
                                           labels=variables)
          [txt.set_rotation(angle-90) for txt, angle 
               in zip(text, angles)]
          for ax in axes[1:]:
              ax.patch.set_visible(False)
              ax.grid("off")
              ax.xaxis.set_visible(False)
          for i, ax in enumerate(axes):
              grid = np.linspace(*ranges[i], 
                                 num=n_ordinate_levels)
              gridlabel = ["{}".format(round(x,2)) 
                           for x in grid]
              if ranges[i][0] &gt; ranges[i][1]:
                  grid = grid[::-1] # hack to invert grid
                            # gridlabels aren't reversed
              gridlabel[0] = "" # clean up origin
              ax.set_rgrids(grid, labels=gridlabel,
                           angle=angles[i])
              #ax.spines["polar"].set_visible(False)
              ax.set_ylim(*ranges[i])
          # variables for plotting
          self.angle = np.deg2rad(np.r_[angles, angles[0]])
          self.ranges = ranges
          self.ax = axes[0]
      def plot(self, data, *args, **kw):
          sdata = _scale_data(data, self.ranges)
          self.ax.plot(self.angle, np.r_[sdata, sdata[0]], *args, **kw)
      def fill(self, data, *args, **kw):
          sdata = _scale_data(data, self.ranges)
          self.ax.fill(self.angle, np.r_[sdata, sdata[0]], *args, **kw)
  </code></pre>
  
  <p>Every plot represents to different people. I want to add a legend showing the name of everyone and the color that represents him in the plot. I tried to add labels and then the legends but it didn´t work. The plot produced is something like that:</p>
  
  <pre><code>df = pd.DataFrame({
      "Spe": pd.Series([89, 83, 70, 60, 30, 49, 28]),
      "Str": pd.Series([69, 53, 30, 20, 10, 29, 48]),
      "Det": pd.Series([82, 44, 79, 39, 20, 10, 85]),
      "Extr": pd.Series([59, 74, 29, 36, 18, 29, 18]),
      "Int": pd.Series([63, 11, 20, 36, 97, 58, 91]),
      "Est": pd.Series([12, 69, 89, 59, 19, 58, 98]),
      "Ape": pd.Series([29, 13, 94, 30, 20, 10, 67]),
      "ID": pd.Series(["Carl","Michael","Peter","Louis","Sarah",
                      "Laura","Nicholas"])
  })
  dfo = df.drop('ID', 1)
  print(dfo)
  
  variables = ("1", "2", "3", 
          "4", "5", "6", "7")
  ranges = [(1,100),(1,100),(1,100),(1,100),(1,100),(1,100),(1,100)] 
  fig1 = plt.figure(figsize=(8, 8))
  radar = ComplexRadar(fig1, variables, ranges)
  for i in range(len(dfo.index)-1):
      data=dfo.iloc[i,:]
      radar.plot(data)
      radar.fill(data,alpha=0.2)
  sns.plt.show()
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/yiK1Q.png" rel="nofollow">Link to plot produced</a></p>
  
  <p>Thanks for your help.</p>