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

การทำ Annotation Subplots ด้วย A, B, C ใน Matplotlib


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

  • กำหนดขนาดรูปภาพและปรับระยะห่างระหว่างและรอบๆ subplots
  • สร้าง figure และชุด subplots nrows=1, ncols=3
  • สร้าง 1D iterator จากอาร์เรย์
  • วนลูปแต่ละ axes แสดงข้อมูลเป็นภาพ
  • ในลูป วางข้อความ A, B, C
  • ใช้เมธอด show() เพื่อแสดงรูปภาพ

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

import numpy as np
from matplotlib import pyplot as plt
import string

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

fig, axs = plt.subplots(1, 3)
axs = axs.flat
for index, ax in enumerate(axs):
    ax.imshow(np.random.randn(4, 4), interpolation='nearest', cmap="copper")
    ax.text(0.45, 1.1, string.ascii_uppercase[index], transform=ax.transAxes, size=20, weight='bold')
plt.show()

ผลลัพธ์

การทำ Annotation Subplots ด้วย A, B, C ใน Matplotlib