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

วิธีเปลี่ยนความหนาของ Hatch ใน Matplotlib

ตั้งค่าความหนาของ hatch pattern ด้วย plt.rcParams['hatch.linewidth'] = ค่า ก่อนพลอต แล้วใช้ hatch='/' ใน plt.scatter() พร้อม marker='s' (สี่เหลี่ยม)

ขั้นตอนการทำงาน

  1. ตั้งค่าขนาดรูปและเลย์เอาต์
  2. สร้างข้อมูล x, y = sin(x)
  3. ตั้งค่า hatch linewidth: plt.rcParams['hatch.linewidth'] = 1
  4. พลอต scatter marker สี่เหลี่ยม มี hatch='/', facecolor, alpha
  5. แสดงผล

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

import numpy as np
from matplotlib import pyplot as plt

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

x = np.linspace(-5, 5, 25)
y = np.sin(x)

plt.rcParams['hatch.linewidth'] = 1
plt.scatter(x, y, s=700, marker='s', linewidth=0.05, facecolor='red', hatch='/', alpha=0.7)
plt.show()

ผลลัพธ์

วิธีเปลี่ยนความหนาของ Hatch ใน Matplotlib