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

พล็อตกราฟด้วย Seaborn ใน Python

ไลบรารี Plotly สำหรับ Python ทำให้สร้างกราฟแบบโต้ตอบคุณภาพสูงได้ออนไลน์ ใช้สำหรับ line plots, scatter plots, area charts, bar charts, error bars, box plots, histograms, heatmaps, subplots, multiple-axes, polar charts, และ bubble charts

Seaborn เป็นไลบรารีสำหรับสร้างกราฟสถิติใน Python สร้างบน matplotlib และรองรับโครงสร้างข้อมูล pandas

พล็อตกราฟด้วย Seaborn ใน Python

1. นำเข้า seaborn ซึ่งเป็นไลบรารีเดียวที่จำเป็นสำหรับตัวอย่างง่ายนี้

import seaborn as sns

2. ใช้ธีม ค่าสเกล และพาเล็ตสีเริ่มต้นของ seaborn

sns.set()

3. โหลดชุดข้อมูลตัวอย่างหนึ่งชุด

tips = sns.load_dataset("tips")

4. วาด scatter plot แบบมีหลายตัวแปรเชิงความหมาย

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

# โปรแกรม Python วาด scatter plot ด้วย Seaborn
# นำเข้าโมดูล
import matplotlib.pyplot as plt
import seaborn as sns

# ค่าสำหรับแกน x
x = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday']
# ค่าสำหรับแกน y
y = [10.5, 12.5, 11.4, 11.2, 9.2, 14.5, 10.1]

# พล็อตด้วย seaborn
my_plot = sns.stripplot(x, y)

# กำหนดฉลากแกน x และ y
my_plot.set(xlabel='ชื่อวัน', ylabel='ยอดขาย (ล้านดอลลาร์)')

# กำหนดหัวข้อกราฟ
plt.title('Scatter Plot')

# แสดงกราฟ
plt.show()

ผลลัพธ์

พล็อตกราฟด้วย Seaborn ใน Python