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

วิธีทำให้ XKCD Font ทำงานใน Matplotlib

ในการทำให้ xkcd font ทำงาน เราสามารถใช้ plt.xkcd() เพื่อเปิดโหมดการวาดแบบสเก็ตช์ (sketch-style drawing mode)

ขั้นตอน

  • ตั้งค่าขนาดรูปและปรับ padding ระหว่างและรอบ subplots
  • สร้างจุดข้อมูล x และ y ด้วย numpy
  • ใช้ plt.xkcd() เพื่อเปิดโหมดการวาดแบบสเก็ตช์
  • สร้าง figure ใหม่หรือเปิดใช้งาน figure ที่มีอยู่
  • เพิ่ม axis ให้กับ figure เป็นส่วนหนึ่งของ subplot arrangement
  • พลอต x และ y ด้วยเมธอด plot()
  • วางข้อความและ title บน plot
  • เพื่อแสดงรูป ใช้เมธอด show()

ตัวอย่าง

from matplotlib import pyplot as plt
import numpy as np

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

x = np.arange(10)
y = np.sin(x)

plt.xkcd()
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
ax.plot(x, y)
ax.text(4, 4, '(4, 4)', size=16)
plt.title('Y=sin(x)')
plt.show()

ผลลัพธ์

วิธีทำให้ XKCD Font ทำงานใน Matplotlib