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

Histogram tròn (polar) trong Python

Để vẽ histogram tròn (polar):

  • Tạo điểm dữ liệu theta, radii, width bằng numpy.
  • Thêm subplot với projection='polar', nrows=1, ncols=1, index=1.
  • Vẽ biểu đồ cột bằng bar() với theta, radii, width.
  • Lặp radii và bars zip, đặt màu face và alpha. Alpha càng nhỏ càng trong suốt.
  • Hiển thị bằng show().

Ví dụ

import numpy as np
import matplotlib.pyplot as plt
plt.rcParams["figure.figsize"] = [7.00, 3.50]
plt.rcParams["figure.autolayout"] = True
N = 20
theta = np.linspace(0.0, 2 * np.pi, N, endpoint=False)
radii = 10 * np.random.rand(N)
width = np.pi / 4 * np.random.rand(N)
ax = plt.subplot(111, projection='polar')
bars = ax.bar(theta, radii, width=width, bottom=0.0)
for r, bar in zip(radii, bars):
    bar.set_facecolor(plt.cm.rainbow(r / 10.0))
    bar.set_alpha(0.5)
plt.show()

Kết quả

Histogram tròn (polar) trong Python