ในการเพิ่มข้อความที่มีคำอธิบายประกอบตัวหนาใน matplotlib เราสามารถใช้การแสดง LaTeX สำหรับป้ายกำกับ
ขั้นตอน
-
กำหนดขนาดรูปและปรับช่องว่างภายในระหว่างและรอบๆ แผนผังย่อย
-
สร้าง x และ ย จุดข้อมูลโดยใช้ numpy
-
หากต้องการกำหนดป้ายกำกับสำหรับแต่ละจุดที่กระจัดกระจาย ให้สร้างรายการป้ายกำกับ
-
พล็อต xpoints, ypoints โดยใช้ scatter() กระบวนการ. สำหรับสี ให้ใช้ xpoints
-
ทำซ้ำ ป้ายกำกับ xpoints และ คะแนน .
-
ใช้ คำอธิบายประกอบ() เมธอดที่มีการแสดง LaTeX ตัวหนา insie for loop
-
หากต้องการแสดงรูป ให้ใช้ show() วิธีการ
ตัวอย่าง
import numpy as np
from matplotlib import pyplot as plt
plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True
xpoints = np.linspace(1, 10, 10)
ypoints = np.random.rand(10)
labels = ["%.2f" % i for i in xpoints]
plt.scatter(xpoints, ypoints, c=xpoints)
for label, x, y in zip(labels, xpoints, ypoints):
plt.annotate(
f"$\\bf{label}$",
xy=(x, y), xytext=(-20, 20),
textcoords='offset points', ha='center', va='bottom',
arrowprops=dict(arrowstyle='->', connectionstyle='arc3,rad=0'))
plt.show() ผลลัพธ์
