Để thêm legend và tiêu đề cho grouped histograms từ Pandas:
- Thiết lập kích thước hình và điều chỉnh khoảng đệm subplot.
- Tạo DataFrame pandas với khóa "a", "b", "c", "d".
- Vẽ DataFrame với kind="hist".
- Đặt tiêu đề cho axes.
- 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': [1, 1, 1, 1, 3],
'b': [1, 1, 2, 1, 3],
'c': [2, 2, 2, 1, 3],
'd': [2, 1, 2, 1, 3], })
df.plot(kind='hist')
plt.title("Grouped Histograms")
plt.show()
Kết quả
