title: matplotlib - wrap text in legend
body: <p>I am currently trying to plot some <code>pandas</code> data via <code>matplotlib</code>/<code>seaborn</code>, however one of my column titles is particularly long and stretches out the plot. Consider the following example:</p>
  
  <pre><code>import random
  
  import pandas as pd
  import matplotlib.pyplot as plt
  import seaborn as sns
  sns.set_style('darkgrid')
  
  random.seed(22)
  fig, ax = plt.subplots()
  
  df = pd.DataFrame({'Year': [2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016],
                     'One legend label': [random.randint(1,15) for _ in range(10)],
                     'A much longer, much more inconvenient, annoying legend label': [random.randint(1, 15) for _ in range(10)]})
  
  df.plot.line(x='Year', ax=ax)
  ax.legend(bbox_to_anchor=(1, 0.5))
  fig.savefig('long_legend.png', bbox_inches='tight')
  </code></pre>
  
  <p>This produces the following graph:
  <a href="https://i.stack.imgur.com/AW5Hr.png" rel="noreferrer"><img src="https://i.stack.imgur.com/AW5Hr.png" alt="graph with wide legend"></a></p>
  
  <p>Is there any way that I can set the legend entries to wrap, either on a character or a length? I tried to use <code>textwrap</code> to rename the DataFrame columns prior to plotting like so:</p>
  
  <pre><code>import textwrap
  [...]
  renames = {c: textwrap.fill(c, 15) for c in df.columns}
  df.rename(renames, inplace=True)
  [...]
  </code></pre>
  
  <p>However, <code>pandas</code> seemed to ignore the newlines in the column names.</p>