海の伝説を編集する



Edit Seaborn Legend



解決:

もしもlegend_outはに設定されています真実なら伝説は利用可能だg._legendプロパティであり、図の一部です。 Seaborn凡例は、標準のmatplotlib凡例オブジェクトです。したがって、次のような凡例テキストを変更できます。

seabornをsnsとしてインポートtips = sns.load_dataset( 'tips')g = sns.lmplot(x = 'total_bill'、y = 'tip'、hue = 'smoker'、data = Tips、markers = ['o'、 ' x ']、legend_out = True)#title new_title =' My title'g._legend.set_title(new_title)#ラベルを置き換えるnew_labels = ['label 1'、 'label 2'] for t、l in zip(g._legend .texts、new_labels):t.set_text(l)sns.plt.show()

ここに画像の説明を入力してください



別の状況の場合legend_outはに設定されていますNS。どの軸に凡例があるかを定義する必要があります(以下の例では、これは軸番号0です)。

seabornをsnsとしてインポートtips = sns.load_dataset( 'tips')g = sns.lmplot(x = 'total_bill'、y = 'tip'、hue = 'smoker'、data = Tips、markers = ['o'、 ' x ']、legend_out = False)#軸をチェックし、凡例があるものを見つけますleg = g.axes.flat [0] .get_legend()new_title ='私のタイトル 'leg.set_title(new_title)new_labels = [' label 1 ' 、 'label 2'] for t、l in zip(leg.texts、new_labels):t.set_text(l)sns.plt.show()

ここに画像の説明を入力してください



さらに、両方の状況を組み合わせて、次のコードを使用できます。

seabornをsnsとしてインポートtips = sns.load_dataset( 'tips')g = sns.lmplot(x = 'total_bill'、y = 'tip'、hue = 'smoker'、data = Tips、markers = ['o'、 ' x ']、legend_out = True)#軸をチェックし、g.axes.flatでaxの凡例があるものを見つけます:leg = g.axes.flat [0] .get_legend()legがNoneでない場合:break#またはlegend legがNoneの場合、図上にある可能性があります:leg = g._legend#凡例テキストを変更new_title = 'マイタイトル' leg.set_title(new_title)new_labels = ['label 1'、 'label 2'] for t、l in zip (leg.texts、new_labels):t.set_text(l)sns.plt.show()

このコードは、に基づいているすべての海のプロットで機能しますグリッドクラス。


上記を読むのに少し時間がかかりました。これが私にとっての答えでした:



インポートseabornas sns import matplotlib.pyplot as plt Tips = sns.load_dataset( 'tips')g = sns.lmplot(x = 'total_bill'、y = 'tip'、hue = 'smoker'、data = Tips、legend = False)plt.legend(title = 'Smoker'、loc = 'upper left'、labels = ['Hell Yeh'、 'Nah Bruh'])plt.show(g)

その他の引数については、これを参照してください:matplotlib.pyplot.legend

ここに画像の説明を入力してください


凡例のタイトルを変更したいだけの場合は、次のようにすることができます。

インポートseabornas sns import matplotlib.pyplot as plt Tips = sns.load_dataset( 'tips')g = sns.lmplot(x = 'total_bill'、y = 'tip'、hue = 'smoker'、data = Tips、legend = True)g._legend.set_title( 'New Title')