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

วิธีเติมพื้นที่ด้วย hatch เท่านั้น (ไม่มีสีพื้นหลัง) ใน matplotlib 2.0

ในการเติมพื้นที่ด้วย hatch เท่านั้น (ไม่มีสีพื้นหลัง) ใน matplotlib สามารถทำตามขั้นตอนดังนี้:

ขั้นตอน

  • ตั้งค่าขนาดรูปและปรับ padding ระหว่างและรอบ subplots

  • เริ่มต้นตัวแปร n เก็บจำนวนข้อมูลตัวอย่าง

  • สร้าง figure และชุด subplots

  • วาดข้อมูล x และ y

  • เติมพื้นที่ระหว่าง x และ y ด้วย hatch วงกลม edgecolor="blue"

  • แสดงรูปด้วยเมธอด show()

ตัวอย่าง

import numpy as np
import matplotlib.pyplot as plt
# ตั้งค่าขนาดรูป
plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True
# จำนวนข้อมูลตัวอย่าง
n = 256
# x และ y data points
x = np.linspace(-np.pi, np.pi, n, endpoint=True)
y = np.sin(2 * x)
# Figure และ subplots
fig, ax = plt.subplots()
# วาดข้อมูล
ax.plot(x, y, color='blue', alpha=1.0)
# เติมพื้นที่ระหว่างข้อมูล
ax.fill_between(x, y, color='blue', alpha=.2, facecolor="none", hatch="o", edgecolor="blue", linewidth=1.0)
# แสดง plot
plt.show()

ผลลัพธ์

จะแสดงโค้ง sin(2x) สีน้ำเงิน พื้นที่ใต้โค้งมี hatch วงกลม สีน้ำเงิน ไม่มีสีเติม

วิธีเติมพื้นที่ด้วย hatch เท่านั้น (ไม่มีสีพื้นหลัง) ใน matplotlib 2.0