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

วิธีพล็อต Jointplot พร้อม Parameter hue ใน Seaborn

การใช้ hue ใน Seaborn Jointplot

jointplot รองรับ parameter hue สำหรับจัดสีตามกลุ่มข้อมูล

ขั้นตอนหลัก

  • ตั้งค่าขนาดรูปและ padding
  • สร้างข้อมูล x ด้วย numpy
  • สร้าง dictionary ข้อมูล curve ต่างๆ
  • แปลงเป็น DataFrame
  • สร้าง jointplot ด้วย jointplot() ระบุ hue
  • แสดงผลด้วย show()

ตัวอย่างโค้ด

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

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

x = np.linspace(0, 1, 5)
d = {
    'y=sin(x)': np.sin(x),
    'y=cos(x)': np.cos(x),
    'y=x': x,
}
df = pd.DataFrame(d)

jg = sns.jointplot(data=df, x="y=sin(x)", y="y=cos(x)",
                   height=3.5, hue="y=x")
plt.show()

คำอธิบาย

hue="y=x" ใช้คอลัมน์ y=x เป็นตัวกำหนดสีของจุดข้อมูล