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

วิธีบันทึกกราฟใน Seaborn ด้วย Python (Matplotlib)


เพื่อบันทึกกราฟใน Seaborn ใช้เมธอด savefig()

ขั้นตอน

  • ตั้งค่าขนาดรูปและ padding ระหว่าง subplots
  • สร้างข้อมูลตาราง 2 มิติ (DataFrame)
  • พลอตความสัมพันธ์คู่ไปคู่มา (pairplot)
  • บันทึกกราฟเป็นไฟล์ด้วย savefig()
  • แสดงรูปด้วย show()

ตัวอย่าง

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

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

df = pd.DataFrame(np.random.random((5, 5)), columns=["a", "b", "c", "d", "e"])
sns_pp = sns.pairplot(df)
sns_pp.savefig("sns-heatmap.png")

ผลลัพธ์

โค้ดจะสร้างกราฟและบันทึกเป็น "sns-heatmap.png" ในโฟลเดอร์โปรเจกต์

วิธีบันทึกกราฟใน Seaborn ด้วย Python (Matplotlib)