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

วิธีจัดรูปแบบ float ด้วย LaTeX formatter ของ matplotlib

ในการจัดรูปแบบ float ด้วย LaTeX formatter ของ matplotlib สามารถทำตามขั้นตอนดังนี้:

ขั้นตอน

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

  • สร้างข้อมูล x และ y ด้วย numpy

  • วาดข้อมูล x, y ด้วยเมธอด plot()

  • เติมสีพื้นที่ใต้เส้นโค้ง

  • ตั้งค่าชื่อรูปด้วย LaTeX representation

  • แสดงรูปด้วยเมธอด 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 และ y data points
x = np.linspace(-5, 5, 100)
y = x**3/3
# วาดข้อมูล
plt.plot(x, y)
# เติมสีพื้นที่ใต้โค้ง
plt.fill_between(x, y)
# LaTex representation
plt.title("$area=\int_a^b{x^2dx}$=83.3")
# แสดง plot
plt.show()

ผลลัพธ์

จะแสดงกราฟ y=x^3/3 เติมสีใต้โค้ง ชื่อรูปเป็นสูตร LaTeX สวยงาม

วิธีจัดรูปแบบ float ด้วย LaTeX formatter ของ matplotlib