Dùng np.histogram() để lấy hist (tần suất) và edges (biên bins), sau đó vẽ bar chart.
- a = [-0.125, .15, 8.75, 72.5, -44.245, 88.45].
- bins = np.arange(-180, 181, 20).
- hist, edges = np.histogram(a, bins).
- freq = hist / hist.sum().
- plt.bar(bins[:-1], freq, width=20, align="edge", ec="k", color='red').
- show().
Ví dụ
import numpy as np from matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True a = [-0.125, .15, 8.75, 72.5, -44.245, 88.45] bins = np.arange(-180, 181, 20) hist, edges = np.histogram(a, bins) freq = hist/float(hist.sum()) plt.bar(bins[:-1], freq, width=20, align="edge", ec="k", color='red') plt.show()
Kết quả
Biểu đồ cột (bar) thể hiện tần suất tương đối (frequency) cho các bins độ rộng 20.