如何在 Seaborn 點圖上獲取資料標籤?


若要在 Seaborn 點圖上獲取資料標籤,我們可以採取以下步驟 −

步驟

  • 設定圖形大小,並調整子圖之間和周圍的填充。

  • 建立一個 df 資料框,該資料框是二維的、可變大小的、潛在的異構表格資料。

  • 建立一個點圖。

  • 獲取軸補丁和標籤;用各個標籤註釋。

  • 若要顯示圖形,可以使用 show() 方法。

示例

from matplotlib import pyplot as plt
import pandas as pd
import seaborn as sns

plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True

df = pd.DataFrame({'a': [1, 3, 1, 2, 3, 1]})

ax = sns.pointplot(df["a"],
   order=df["a"].value_counts().index)

for p, label in zip(ax.patches, df["a"].value_counts().index):
   ax.annotate(label, (p.get_x() + 0.375, p.get_height() + 0.15))

plt.show()

輸出

它將生成以下輸出 −

更新日期:02-02-2022

3K+ 瀏覽

開啟你的職業生涯

完成課程即可獲得認證

立即開始
廣告