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

วิธีปรับแต่งป้ายแกนใน Seaborn jointplot ด้วย Matplotlib

การปรับแต่งป้ายแกนใน Seaborn jointplot สามารถทำได้โดยใช้เมธอด set_xlabel() และ set_ylabel() ของออบเจ็กต์ JointGrid ที่คืนค่ากลับมา นอกจากนี้ยังรองรับรูปแบบ LaTeX สำหรับการแสดงสูตรคณิตศาสตร์หรือข้อความแบบตัวหนาได้

ขั้นตอนการทำงาน

  1. ตั้งค่าขนาดรูปและระยะห่างระหว่างซับพลอตด้วย plt.rcParams
  2. สร้างข้อมูล x และ y ด้วย NumPy
  3. สร้าง jointplot ด้วย sns.jointplot()
  4. ปรับแต่งป้ายแกน x ด้วย ax_joint.set_xlabel() (รองรับ LaTeX)
  5. แสดงผลด้วย plt.show()

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

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

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

x = np.random.randn(1000)
y = 0.2 * np.random.randn(1000) + 0.5

h = sns.jointplot(x, y, height=3.50)
h.ax_joint.set_xlabel(r'$\bf{X-Axis\ Label}$')
plt.show()

ผลลัพธ์

วิธีปรับแต่งป้ายแกนใน Seaborn jointplot ด้วย Matplotlib