Để hiển thị bảng dữ liệu (DataFrame) và biểu đồ cạnh nhau trong một figure, ta chia subplot 1 hàng 2 cột: cột 1 vẽ biểu đồ, cột 2 vẽ bảng bằng ax.table().
Các bước
- Thiết lập figure size và autolayout.
- Tạo DataFrame với cột Straight, Square.
- Tạo figure, thêm subplot 121 cho scatter plot.
- Thêm subplot 122, tắt axes, vẽ bảng bằng ax.table(cellText=df.values, ...).
- Hiển thị bằng show().
Ví dụ
import matplotlib.pyplot as plt
import pandas as pd
plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True
df = pd.DataFrame({'Straight': [i for i in range(10)], 'Square': [i * i for i in range(10)]})
fig = plt.figure()
ax1 = fig.add_subplot(121)
ax1.scatter(x=df.Straight, y=df.Square)
ax2 = fig.add_subplot(122)
font_size = 14
bbox = [0, 0, 1, 1]
ax2.axis('off')
mpl_table = ax2.table(cellText=df.values, rowLabels=df.index, bbox=bbox, colLabels=df.columns)
plt.show()
Kết quả
