Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> Python

Vẽ biến phân loại (categorical variables) trong Matplotlib


Các bước

  1. Thiết lập kích thước hình và padding.
  2. Tạo dictionary dữ liệu.
  3. Trích xuất keys và values.
  4. Tạo figure và subplots (1 hàng, 3 cột).
  5. Vẽ bar, scatter, plot với names và values.
  6. Gọi show().

Ví dụ

import matplotlib.pyplot as plt

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

data = {'apple': 10, 'orange': 15, 'lemon': 5}
names = list(data.keys())
values = list(data.values())

fig, axs = plt.subplots(1, 3)
axs[0].bar(names, values)
axs[1].scatter(names, values)
axs[2].plot(names, values)
plt.show()

Kết quả

Vẽ biến phân loại (categorical variables) trong Matplotlib