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

วิธีพลอต Scatter จุดที่ถูก Mask และเพิ่มเส้นแบ่งพื้นที่ Mask ใน Matplotlib


เพื่อพลอต scattered masked points และเพิ่มเส้นแบ่งพื้นที่ที่ถูก mask

ขั้นตอน

  • ตั้งค่าขนาดรูปและ padding ระหว่าง subplots
  • สร้าง N, r0, x, y, area, c, r, area1, area2 ด้วย numpy
  • พลอต x และ y ด้วย scatter()
  • เพิ่มเส้นแบ่งพื้นที่ mask ด้วย plot()
  • แสดงรูปด้วย show()

ตัวอย่าง

import matplotlib.pyplot as plt
import numpy as np

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

N = 100
r0 = 0.6
x = 0.9 * np.random.rand(N)
y = 0.9 * np.random.rand(N)
area = (20 * np.random.rand(N))**2
c = np.sqrt(area)
r = np.sqrt(x**2 + y**2)
area1 = np.ma.masked_where(r < r0, area)
area2 = np.ma.masked_where(r >= r0, area)

plt.scatter(x, y, s=area1, marker='^', c=c)
plt.scatter(x, y, s=area2, marker='o', c=c)
theta = np.arange(0, np.pi/2, 0.01)
plt.plot(r0 * np.cos(theta), r0 * np.sin(theta))
plt.show()

ผลลัพธ์

วิธีพลอต Scatter จุดที่ถูก Mask และเพิ่มเส้นแบ่งพื้นที่ Mask ใน Matplotlib