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

การหมุนข้อความแกนสำหรับแต่ละ subplot ใน Matplotlib


หมุนข้อความแกนในแต่ละ subplot

ใช้ text พร้อมพารามิเตอร์ rotation เพื่อหมุนข้อความในแต่ละ subplot

ขั้นตอน

  1. สร้าง figure ใหม่
  2. เพิ่ม axes ด้วย add_subplot()
  3. ปรับ layout ด้วย subplots_adjust()
  4. เพิ่ม suptitle ด้วย suptitle()
  5. ตั้งค่า title, xlabel, ylabel
  6. สร้างแกนพร้อมพิกัด
  7. เพิ่มข้อความพร้อม fontsize, fontweight, rotation
  8. วาดจุดเดียวและ annotate พร้อมลูกศร
  9. แสดงด้วย show()

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

from matplotlib import pyplot as plt

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

fig = plt.figure()
ax = fig.add_subplot()
fig.subplots_adjust(top=0.85)
fig.suptitle('bold figure suptitle', fontsize=14, fontweight='bold')
ax.set_title('axes title')
ax.set_xlabel('xlabel')
ax.set_ylabel('ylabel')
ax.axis([0, 10, 0, 10])

ax.text(3, 8, 'boxed italics text in data coords', style='italic', 
        bbox={'facecolor': 'red', 'alpha': 0.5, 'pad': 10})
ax.text(2, 6, r'an equation: $E=mc^2$', fontsize=15)
ax.text(3, 2, 'unicode: Institut f\u00fcr Festk\u00f6rperphysik')
ax.text(0.95, 0.01, 'colored text in axes coords', 
        verticalalignment='bottom', horizontalalignment='right',
        transform=ax.transAxes, color='green', fontsize=15)

ax.plot([2], [1], 'o')
ax.annotate('annotate', xy=(2, 1), xytext=(3, 4), 
            arrowprops=dict(facecolor='black', shrink=0.05))

plt.show()

ผลลัพธ์

การหมุนข้อความแกนสำหรับแต่ละ subplot ใน Matplotlib