Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> Python

Lấy nhãn dữ liệu trên Seaborn pointplot trong Python


Để lấy nhãn dữ liệu trên Seaborn pointplot, thực hiện các bước sau:

Các bước

  • Thiết lập kích thước hình và padding.
  • Tạo DataFrame df dữ liệu 2 chiều.
  • Tạo pointplot.
  • Lấy axes patches và nhãn; annotate với nhãn tương ứng.
  • Hiển thị hình bằng show().

Ví dụ

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()

Kết quả

Hiển thị pointplot với nhãn số lượng trên mỗi điểm.