การเพิ่ม Hatching Patterns ให้ Contour Plot
ใช้ parameter hatches ใน contourf() เพื่อเพิ่มรูปแบบเส้นขีด
ขั้นตอนหลัก
- ตั้งค่าขนาดรูปและ padding
- สร้างข้อมูล x, y, z ด้วย numpy meshgrid
- Flatten x, y สำหรับ contourf
- สร้าง figure และ axes
- พล็อต contourf() พร้อม hatches=['-', '/', '\\', '//']
- เพิ่ม colorbar
- แสดงผลด้วย show()
ตัวอย่างโค้ด
import matplotlib.pyplot as plt
import numpy as np
plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True
x = np.linspace(-3, 5, 150).reshape(1, -1)
y = np.linspace(-3, 5, 120).reshape(-1, 1)
z = np.cos(x) + np.sin(y)
x, y = x.flatten(), y.flatten()
fig1, ax1 = plt.subplots()
cs = ax1.contourf(x, y, z, hatches=['-', '/', '\\', '//'],
cmap='gray', extend='both', alpha=0.5)
fig1.colorbar(cs)
plt.show()
Hatch Patterns
- -: เส้นขวาง
- /: เส้นทแยงมุมขวา
- \\: เส้นทแยงมุมซ้าย
- //: เส้นทแยงมุมขวาหนา
- อื่นๆ: |, +, x, o, O, ., *