Bài 11: Grids – Matplotib Cơ Bản

Trang chủ » Training » Bài 11: Grids – Matplotib Cơ Bản
26/02/2022 Training 94 viewed

1. Khái niệm cơ bản :

Hàm Grid () của trục được đặt hiển thị bên trong lưới để bật hoặc tắt. Bạn cũng có thể hiển thị các tích tắc chính / phụ (hoặc cả hai) của lưới. Ngoài ra, các thuộc tính màu sắc, kiểu đường kẻ và độ rộng đường thẳng có thể được đặt trong hàm grid ().
import matplotlib.pyplot as plt
import numpy as np
fig, axes = plt.subplots(1,3, figsize = (12,4))
x = np.arange(1,11)
axes[0].plot(x, x**3, 'g',lw=2)
axes[0].grid(True)
axes[0].set_title('default grid')
axes[1].plot(x, np.exp(x), 'r')
axes[1].grid(color='b', ls = '-.', lw = 0.25)
axes[1].set_title('custom grid')
axes[2].plot(x,x)
axes[2].set_title('no grid')
fig.tight_layout()
plt.show()
Các tham số cần chú ý :
  • :  bool hoặc None
  • which :  {‘major’, ‘minor’, ‘both’} Các đường lưới để áp dụng các thay đổi trên.
  • axis : {‘both’, ‘x’, ‘y’} Trục áp dụng các thay đổi trên.

2. Các ví dụ :

Ví dụ 1 : Nan Test
import numpy as np
import matplotlib.pyplot as plt

t = np.arange(0.0, 1.0 + 0.01, 0.01)
s = np.cos(2 * 2*np.pi * t)
t[41:60] = np.nan

plt.subplot(2, 1, 1)
plt.plot(t, s, '-', lw=2)

plt.xlabel('time (s)')
plt.ylabel('voltage (mV)')
plt.title('A sine wave with a gap of NaNs between 0.4 and 0.6')
plt.grid(True)

plt.subplot(2, 1, 2)
t[0] = np.nan
t[-1] = np.nan
plt.plot(t, s, '-', lw=2)
plt.title('Also with NaN in first and last point')

plt.xlabel('time (s)')
plt.ylabel('more nans')
plt.grid(True)

plt.tight_layout()
plt.show()
Kết quả :
Ví dụ 2 : Geographic Projections 
Điều này cho thấy 4 phép chiếu có thể sử dụng subplot. Matplotlib cũng hỗ trợ Basemaps Toolkit và Cartopy cho các phép chiếu địa lý.
import matplotlib.pyplot as plt
plt.figure()
plt.subplot(111, projection="aitoff")
plt.title("Aitoff")
plt.grid(True)
plt.figure()
plt.subplot(111, projection="hammer")
plt.title("Hammer")
plt.grid(True)
plt.figure()
plt.subplot(111, projection="lambert")
plt.title("Lambert")
plt.grid(True)
plt.figure()
plt.subplot(111, projection="mollweide")
plt.title("Mollweide")
plt.grid(True)

plt.show()
Chia sẻ:
Tags:
TOP HOME