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

Vẽ đường cong với style fivethirtyeight trong Matplotlib


Các bước

  1. Thiết lập kích thước hình và padding.
  2. Áp dụng style fivethirtyeight bằng plt.style.use().
  3. Tạo dữ liệu x bằng numpy.
  4. Tạo figure và subplots.
  5. Vẽ ba đường cong bằng plot().
  6. Đặt tiêu đề.
  7. Gọi show().

Ví dụ

import matplotlib.pyplot as plt
import numpy as np

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

plt.style.use('fivethirtyeight')
x = np.linspace(0, 10)
fig, ax = plt.subplots()
ax.plot(x, np.sin(x) + x + np.random.randn(50))
ax.plot(x, np.sin(x) + 0.5 * x + np.random.randn(50))
ax.plot(x, np.sin(x) + 2 * x + np.random.randn(50))
ax.set_title("'fivethirtyeight' style sheet")
plt.show()

Kết quả

Vẽ đường cong với style fivethirtyeight trong Matplotlib