Phần 11: Grids – Matplotib Basic

Phần 11: Grids                        – Matplotib Basic

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

Hàm Grid () của trục được đặt hiển thị bên trong lưới để bật hay tắt. Bạn cũng sẽ có thể hiển thị các vài giây chính / phụ (hay cả 2) 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(shade='b', ls = '-.', lw = 0.25)
axes[1].set_title('customized grid')
axes[2].plot(x,x)
axes[2].set_title('no grid')
fig.tight_layout()
plt.present()
Các tham số cần lưu ý :
  • b :  bool hay None
  • which :   Những đường lưới để áp dụng những thay đổi trên.
  • axis :  Trục áp dụng những 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 spot 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 final level')

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

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

plt.present()

admin

Leave a Reply

Your email address will not be published. Required fields are marked *