Để thêm mẫu vân (hatch texture) cho các cột bar chart, thiết lập tham số hatch trong ax.bar().
Các bước
- Thiết lập figure size và autolayout.
- Tạo figure và axes.
- Chuẩn bị danh sách các mẫu hatch (//, *, o, d, .).
- Vẽ từng cột trong vòng lặp với hatch khác nhau.
- Hiển thị bằng show().
Ví dụ
import numpy as np
from matplotlib import pyplot as plt
plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True
fig = plt.figure()
ax = fig.add_subplot(111)
textures = ["//", "*", "o", "d", "."]
for i in range(len(textures)):
ax.bar(i, np.random.randint(1, 5), color="green", edgecolor="black", alpha=0.3, hatch=textures[i])
plt.show()
Kết quả
