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

วิธีวาดหัวใจด้วย pylab

ในการวาดหัวใจด้วย pylab/pyplot เราสามารถทำตามขั้นตอนด้านล่างได้:

ขั้นตอน

  • ตั้งค่าขนาดรูปและปรับ padding ระหว่างและรอบ subplots
  • สร้างจุดข้อมูล x, y1 และ y2 ด้วย numpy
  • เติมสีพื้นที่ระหว่าง (x, y1) และ (x, y2) ด้วยเมธอด fill_between()
  • วางข้อความบน plot ด้วยเมธอด text() ที่จุด (0, -1.0)
  • เพื่อแสดงรูป ใช้เมธอด show()

ตัวอย่าง

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(-2, 2, 1000)
y1 = np.sqrt(1 - (np.abs(x) - 1) ** 2)
y2 = -3 * np.sqrt(1 - (np.abs(x) / 2) ** 0.5)

plt.fill_between(x, y1, color='red')
plt.fill_between(x, y2, color='red')
plt.text(0, -1.0, 'Heart', fontsize=24, color='black', horizontalalignment='center')
plt.show()

ผลลัพธ์

วิธีวาดหัวใจด้วย pylabวิธีวาดหัวใจด้วย pylab