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

เพิ่มเส้นบน Scatter Plot ด้วย Python Matplotlib


เพื่อเพิ่มเส้นบน Scatter Plot ด้วย Matplotlib

  • ตั้งค่าขนาดรูปและ padding ระหว่าง subplots
  • กำหนดตัวแปร n จำนวนจุดข้อมูล
  • พลอต x และ y ด้วย scatter()
  • พลอตเส้นด้วย plot()
  • จำกัดแกน X ด้วย xlim()
  • แสดงรูปด้วย show()

ตัวอย่าง

import numpy as np
from matplotlib import pyplot as plt

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

n = 100
x = np.random.rand(n)
y = np.random.rand(n)

plt.scatter(x, y, c=x)
plt.plot([0.1, 0.4, 0.3, 0.2])
plt.xlim(0, 1)
plt.show()

ผลลัพธ์

เพิ่มเส้นบน Scatter Plot ด้วย Python Matplotlib