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

การใช้ Superscript ในกราฟ Python (Matplotlib)


ขั้นตอนการทำ Superscript ใน Python

  • สร้างจุดสำหรับ a และ f ด้วย NumPy
  • พล็อตกราฟ f = ma ด้วยเมธอด plot() พร้อม label f=ma
  • เพิ่ม Title พร้อม Superscript เช่น kgms-2
  • เพิ่ม xlabel พร้อม Superscript เช่น ms-2
  • เพิ่ม ylabel พร้อม Superscript เช่น kg
  • ใช้เมธอด legend() เพื่อวาง Legend
  • ใช้เมธอด show() เพื่อแสดง Figure

ตัวอย่างโค้ด

import numpy as np
from matplotlib import pyplot as plt
plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True
a = np.linspace(1, 10, 100)
m = 20
f = m*a
plt.plot(a, f, c="red", lw=5, label="f=ma")
plt.title("Force $\mathregular{kgms^{-2}}$")
plt.xlabel("Acceleration $\mathregular{ms^{-2}}$")
plt.ylabel("Acceleration $\mathregular{kg}$")
plt.legend()
plt.show()

ผลลัพธ์

การใช้ Superscript ในกราฟ Python (Matplotlib)