seaborn.move_legend#
- seaborn.move_legend(obj, loc, **kwargs)#
プロットの凡例を新しい場所に再作成します。
名前は少し不正確です。Matplotlib の凡例は、位置パラメータに対するパブリックコントロールを公開していません。したがって、この関数は、元のオブジェクトからデータをコピーし、削除された新しい凡例を作成します。
- パラメータ:
- objプロットを持つオブジェクト
この引数は、seaborn または matplotlib のオブジェクトのいずれかになります。
- locstr または int
matplotlib.axes.Axes.legend()
のように、位置引数。- kwargs
その他のキーワード引数は、
matplotlib.axes.Axes.legend()
に渡されます。
例
軸レベルの関数については、
matplotlib.axes.Axes
オブジェクトを渡し、新しい場所を指定します。ax = sns.histplot(penguins, x="bill_length_mm", hue="species") sns.move_legend(ax, "center right")
凡例を軸の外に移動するなど、より細かい制御を行うには、
bbox_to_anchor
パラメータを使用します。ax = sns.histplot(penguins, x="bill_length_mm", hue="species") sns.move_legend(ax, "upper left", bbox_to_anchor=(1, 1))
他のプロパティを更新するには、追加の
matplotlib.axes.Axes.legend()
パラメータを渡します。ax = sns.histplot(penguins, x="bill_length_mm", hue="species") sns.move_legend( ax, "lower center", bbox_to_anchor=(.5, 1), ncol=3, title=None, frameon=False, )
図レベルの関数によって作成された凡例を移動することもできます。ただし、位置を微調整する場合は、図の右側に余分な空白ができることに注意する必要があります。
g = sns.displot( penguins, x="bill_length_mm", hue="species", col="island", col_wrap=2, height=3, ) sns.move_legend(g, "upper left", bbox_to_anchor=(.55, .45))
これを避ける 1 つの方法は、
FacetGrid
でlegend_out=False
を設定することです。g = sns.displot( penguins, x="bill_length_mm", hue="species", col="island", col_wrap=2, height=3, facet_kws=dict(legend_out=False), ) sns.move_legend(g, "upper left", bbox_to_anchor=(.55, .45), frameon=False)