ในการใส่ข้อความที่มุมของสัดส่วนที่เท่ากันใน matplotlib เราสามารถทำตามขั้นตอนต่อไปนี้ -
-
กำหนดขนาดรูปและปรับช่องว่างภายในระหว่างและรอบๆ แผนผังย่อย
-
สร้างร่างและชุดแผนย่อยโดยใช้ แผนย่อย() วิธีการ
-
สร้างจุดข้อมูล x โดยใช้ numpy
-
พล็อต x บนแกน ax1 โดยใช้ plot() วิธีการ
-
พล็อต x และ 2*x บน ax2 โดยใช้ plot() วิธีการ
-
หากต้องการใส่ข้อความไว้ที่มุมของรูป ให้ใช้ annotate() วิธีการสำหรับแกนต่างๆ
-
หากต้องการแสดงรูป ให้ใช้ show() วิธีการ
ตัวอย่าง
from matplotlib import pyplot as plt
plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True
fig, axes = plt.subplots(2)
x = np.linspace(-2, 2, 10)
axes[0].plot(x)
axes[1].plot(x, 2*x)
for ax in axes:
ax.annotate('Straight Line', xy=(1, 0),xycoords='axes fraction', fontsize=10, horizontalalignment='right', verticalalignment='bottom')
plt.show() ผลลัพธ์
