Đọc file text từng dòng, tách tên và chiều cao, vẽ bar chart.
- bar_names = [], bar_heights = [].
- for line in open("test_data.txt"): name, height = line.split(); append.
- plt.bar(bar_names, bar_heights).
- show().
Ví dụ
from matplotlib import pyplot as plt
plt.rcParams["figure.figsize"] = [7.00, 3.50]
plt.rcParams["figure.autolayout"] = True
bar_names = []
bar_heights = []
for line in open("test_data.txt", "r"):
bar_name, bar_height = line.split()
bar_names.append(bar_name)
bar_heights.append(bar_height)
plt.bar(bar_names, bar_heights)
plt.show()
File "test_data.txt" chứa:
Javed 75 Raju 65 Kiran 55 Rishi 95
Kết quả
Biểu đồ cột 4 cột: Javed(75), Raju(65), Kiran(55), Rishi(95).