pandas
のデータをmatplotlib
/seaborn
でプロットしようとしていますが、私の欄のタイトルの1つが特に長く、プロットが広がっています。次の例を考えてみましょう:matplotlib - 凡例のテキストを折り返し
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')
は、私はどちらかの文字や長さに、ラップに凡例を設定することができます任意の方法はありますか?私は前にそうようにプロットするデータフレームの列の名前を変更するtextwrap
を使用しようとしました:
import textwrap
[...]
renames = {c: textwrap.fill(c, 15) for c in df.columns}
df.rename(renames, inplace=True)
[...]
しかし、pandas
は、列名に改行を無視するように見えました。
あなたは、単純には「\ n」を私が簡略化されてきた –
@JanZeiseweisを追加することができます私が見ているデータはCSVファイルから来ています – asongtoruin