Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python

วิธีได้ป้ายข้อมูลบน Seaborn pointplot ใน Python

ในการได้ป้ายข้อมูล (data labels) บน Seaborn pointplot สามารถทำตามขั้นตอนดังนี้:

ขั้นตอน

  • ตั้งค่าขนาดรูปและปรับ padding ระหว่างและรอบ subplots

  • สร้าง dataframe df ข้อมูลตาราง 2 มิติ ขนาดเปลี่ยนได้

  • สร้าง pointplot

  • ดึง axes patches และ label; annotate ด้วย label ที่สอดคล้อง

  • แสดงรูปด้วยเมธอด 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()

ผลลัพธ์

จะแสดง pointplot พร้อมป้ายข้อมูลบนแต่ละจุด

วิธีได้ป้ายข้อมูลบน Seaborn pointplot ใน Python