首頁  >  文章  >  後端開發  >  如何移動 Seaborn 的 Factorplot 中的圖例?

如何移動 Seaborn 的 Factorplot 中的圖例?

Patricia Arquette
Patricia Arquette原創
2024-10-25 04:43:29937瀏覽

How to Move the Legend in Seaborn's Factorplot?

自訂Seaborn圖例放置

在seaborn的factorplot(kind="bar")中,圖例的位置往往不太理想,尤其是當情節元素擁擠時。本文探討如何將圖例移到更適合的位置,例如左上角。

一種方法是透過設定 legend=False 來阻止seaborn產生圖例。隨後,您可以使用 matplotlib 手動建立圖例:

<code class="python">import seaborn as sns
import matplotlib.pyplot as plt

sns.set(style="whitegrid")

titanic = sns.load_dataset("titanic")

g = sns.factorplot("class", "survived", "sex",
                   data=titanic, kind="bar",
                   size=6, palette="muted",
                   legend=False)
g.despine(left=True)
plt.legend(loc='upper left')
g.set_ylabels("survival probability")</code>

注意: 要與 matplotlib 中的 FacetGrid 軸進行交互,請使用Fig.get_axes()[0]。例如:

<code class="python">g.fig.get_axes()[0].legend(loc='lower left')</code>

以上是如何移動 Seaborn 的 Factorplot 中的圖例?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn