ในการเพิ่มข้อความที่มีคำอธิบายประกอบใน Matplotlib หลายจุด เราสามารถทำตามขั้นตอนต่อไปนี้ -
- กำหนดขนาดรูปและปรับช่องว่างภายในระหว่างและรอบๆ แผนผังย่อย
- สร้างจุดข้อมูล x และ y โดยใช้ numpy
- หากต้องการตั้งค่าป้ายกำกับสำหรับแต่ละจุดที่กระจัดกระจาย ให้สร้างรายการป้ายกำกับ
- พล็อต xpoints, ypoints โดยใช้ scatter() กระบวนการ. สำหรับสี ให้ใช้ xpoints
- ทำซ้ำป้ายกำกับซิป xpoints และ ypoints
- ใช้วิธี annotate() กับการแสดง LaTeX ตัวหนาใน 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() ผลลัพธ์
