Để trả về đối tượng matplotlib.figure.Figure từ hàm plot Pandas:
- Thiết lập kích thước hình và điều chỉnh khoảng đệm subplot.
- Tạo DataFrame pandas df.
- Vẽ biểu đồ cột ngang bằng barh().
- Lấy instance figure hiện tại bằng get_figure().
- Đặt legend ở góc dưới phải.
- Hiển thị hình bằng show().
Ví dụ
from matplotlib import pyplot as plt
import pandas as pd
plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True
df = pd.DataFrame({'a': range(10)})
ax = df.plot.barh(color=(1, 0, 0, 0.25))
fig = ax.get_figure()
ax.legend(loc='lower right')
plt.show()
Kết quả
