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

วิธีเติมสีพื้นที่ใต้เส้นโค้งใน Seaborn Distribution Plot

ในการเติมสีพื้นที่ใต้เส้นโค้งใน Seaborn distribution plot เราสามารถใช้เมธอด distplot() และ fill_between()

ขั้นตอน

  • ตั้งค่าขนาดรูปและปรับ padding ระหว่างและรอบ subplots
  • สร้างลิสต์ของจุดข้อมูล
  • พลอตการกระจายตัวแบบ univariate ของ observations
  • เพื่อเติมพื้นที่ใต้โค้ง ใช้เมธอด fill_between()
  • ตั้งค่าหรือดึง autoscaling margins x=0 และ y=0
  • เพื่อแสดงรูป ใช้เมธอด show()

ตัวอย่าง

import seaborn as sns
import scipy.stats as stats
import matplotlib.pyplot as plt

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

x = [2.0, 7.5, 9.0, 8.5]
ax = sns.distplot(x, fit_kws={"color": "red"}, kde=False, fit=stats.gamma, hist=None, label="label 1")
l1 = ax.lines[0]
x1 = l1.get_xydata()[:, 0]
y1 = l1.get_xydata()[:, 1]
ax.fill_between(x1, y1, color="red", alpha=0.3)
ax.margins(x=0, y=0)
plt.show()

ผลลัพธ์

วิธีเติมสีพื้นที่ใต้เส้นโค้งใน Seaborn Distribution Plotวิธีเติมสีพื้นที่ใต้เส้นโค้งใน Seaborn Distribution Plot