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

วิธีสร้าง Broken Horizontal Bar Plot ใน Matplotlib


ขั้นตอนสร้าง Broken Horizontal Bar Plot:

  • ตั้งค่าขนาดรูปและ padding ระหว่าง subplots
  • สร้าง figure และ subplots
  • พลอตชุดสี่เหลี่ยมแนวนอนด้วย broken_barh()
  • ตั้งค่า scale แกน X, Y, ป้ายแกน X, Y ticks และ labels
  • ตั้งค่า grid
  • ใช้ annotate() แสดงข้อความอ้างอิงตำแหน่ง
  • แสดงรูปด้วย show()

ตัวอย่าง

import matplotlib.pyplot as plt

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

fig, ax = plt.subplots()
ax.broken_barh([(110, 30), (150, 10)], (10, 9), facecolors='tab:blue')
ax.broken_barh([(10, 50), (100, 20), (130, 10)], (20, 9),
    facecolors=('tab:orange', 'tab:green', 'tab:red'))
ax.set_ylim(5, 35)
ax.set_xlim(0, 200)
ax.set_xlabel('seconds since start')
ax.set_yticks([15, 25])
ax.set_yticklabels(['Bill', 'Jim'])
ax.grid(True)
ax.annotate('race interrupted', (61, 25),
    xytext=(0.8, 0.9), textcoords='axes fraction',
    arrowprops=dict(facecolor='black', shrink=0.05),
    fontsize=16,
    horizontalalignment='right', verticalalignment='top')
plt.show()

ผลลัพธ์

วิธีสร้าง Broken Horizontal Bar Plot ใน Matplotlib