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

วิธีได้ขนาดฟอนต์ต่างกันใน Annotation เดียวกันของ Matplotlib


ขั้นตอนการทำงาน

  • สร้างรายการ x และ y สำหรับตำแหน่งข้อความ
  • กำหนดตัวแปร labels เป็นสตริง
  • สร้างรายการขนาดฟอนต์
  • ใช้ subplots() สร้าง figure และ subplots
  • วนลูปรายการทั้งหมด annotate ข้อความแต่ละตัว ตั้งค่า fontsize
  • ใช้เมธอด show() เพื่อแสดงรูปภาพ

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

import matplotlib.pyplot as plt

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

X = [0.1, .2, .3, .4, .5, .6, 0.8]
Y = [0.1, 0.12, 0.13, 0.20, 0.23, 0.25, 0.27]
labels = 'Welcome'
sizes = [10, 20, 30, 40, 50, 60, 70]
fig, ax = plt.subplots()
for x, y, label, size in zip(X, Y, labels, sizes):
    ax.annotate(f"$\it{label}$", (x, y), fontsize=size, color='red')
plt.show()

ผลลัพธ์

วิธีได้ขนาดฟอนต์ต่างกันใน Annotation เดียวกันของ Matplotlib