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

วาด Horizontal Bar Chart 2 ชุด แบบ Share Y-axis ใน Matplotlib

ใช้ subplot() พร้อม sharey=ax1 และเมธอด barh() สำหรับแท่งแนวนอน

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


import matplotlib.pyplot as plt
import numpy as np

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

y = [3, 1, 5]
x1 = [10, 7, 3]
x2 = [9, 5, 1]

fig = plt.figure()
axe1 = plt.subplot(121)
axe1.barh(y, x1, align='center', color='red', edgecolor='black')

axe2 = plt.subplot(122, sharey=axe1)
axe2.barh(y, x2, align='center', color='green', edgecolor='black')

plt.show()

ผลลัพธ์

2 กราฟแท่งแนวนอน ข้างกัน แบ่งปันแกน Y เดียวกัน

วาด Horizontal Bar Chart 2 ชุด แบบ Share Y-axis ใน Matplotlib