簡體   English   中英

如何在Seaborn點圖上獲取數據標簽?

[英]How to get data labels on a Seaborn pointplot?

我有兩個這樣的數組:

Soldier_years = [1850, 1851, 1852, 1853, 1854, 1855, 1856, 1857, 1858, 1859, 1860, 1861, 1862, 1863, 1864, 1865, 1866, 1867, 1868, 1869, 1870]
num_records_yob = [7, 5, 8, 9, 15, 17, 23, 19, 52, 55, 73, 73, 107, 137, 65, 182, 228, 257, 477, 853, 2303]

我試圖將這些變成Seaborn點圖,如下所示:

%matplotlib inline
import seaborn as sns
import matplotlib.pyplot as plt
sns.set(style="darkgrid")

f, (ax) = plt.subplots(figsize=(12, 6), sharex=True)

sns.set_style("darkgrid")
ax = sns.pointplot(x=Soldier_years, y=num_records_yob)

我得到一個像這樣的點圖:

Pointplot

這個情節幾乎就是我想要的。 如何獲得每個點的數據標簽以顯示在各個點之上?

我試過ax.patches ,但它是空的。

我試圖讓它看起來像這樣(但對於點圖): 在此輸入圖像描述

你可以這樣做:

[ax.text(p[0], p[1]+50, p[1], color='g') for p in zip(ax.get_xticks(), num_records_yob)]

情節

為了將來參考,我想建議這個代碼:

ymin, ymax = ax.get_ylim()
color="#3498db" # choose a color
bonus = (ymax - ymin) / 50 # still hard coded bonus but scales with the data
for x, y, name in zip(X, Y, names):
    ax.text(x, y + bonus, name, color=color)

請注意,我還將理解更改為for循環,我認為它更具可讀性(當列表實際被丟棄時)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM