title: Changing the format of labels inside the legends in matplotlib
body: <p>I want to customize the <code>legends</code> in matplotlib. </p>
  
  <p>I want to order the labels inside the legends horizontally and remove the handle as well. Color of the labels must be same as the line color. </p>
  
  <p>Currently I have set the handle as invisible, but not able to change the ordering of labels.</p>
  
  <p>Looking forward for expert advice. </p>
  
  <p><strong>Expected output:</strong></p>
  
  <p><a href="https://i.stack.imgur.com/dwXA3.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/dwXA3.png" alt="enter image description here"></a></p>
  
  <p>What I have achieved so far is the following,</p>
  
  <pre><code>import numpy as np
  import matplotlib.pyplot as plt
  x = np.linspace(0, 20, 1000)
  y1 = np.sin(x)
  y2 = np.cos(x)
  
  plt.figure(figsize=(10,7))
  lines=[]
  lines.append(plt.plot(x, y1, '-b', label='sine')[0])
  lines.append(plt.plot(x, y2, '-r', label='cosine')[0])
  plt.legend(loc='upper left')
  plt.ylim(-1.5, 2.0)
  for item_legend,handle,line in zip(plt.legend().get_texts(),plt.gca().get_legend().legendHandles,lines):
      plt.setp(item_legend, color=line.get_color(),size=30)
      handle.set_visible(False)
  
  plt.show()
  </code></pre>
  
  <p><strong>output:</strong></p>
  
  <p><a href="https://i.stack.imgur.com/RogUT.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/RogUT.png" alt="enter image description here"></a></p>
  
  <p><strong>EDIT:</strong>
  If I set </p>
  
  <pre><code>plt.legend(loc='upper left', ncols=2)
  </code></pre>
  
  <p>Labels are aligned in one row but all the previous formatting gets removed. </p>