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

Mục phóng to (zoomed inset) với tỷ lệ X, Y khác nhau trong Matplotlib

Để hiển thị tỷ lệ X, Y khác nhau trong mục phóng to (zoomed inset) Matplotlib, ta dùng inset_axes().

Các bước

  • Thiết lập kích thước hình và điều chỉnh khoảng đệm subplot.
  • Tạo điểm dữ liệu x, y bằng numpy.
  • Thêm subplot vào figure hiện tại.
  • Vẽ x, y bằng plot().
  • Tạo inset axes với chiều rộng, cao cho trước.
  • Đặt tỷ lệ x, y khác nhau cho inset.
  • Vẽ hộp đánh dấu vùng inset bằng mark_inset().
  • Hiển thị hình bằng show().

Ví dụ

import matplotlib.pyplot as plt
import numpy as np
from mpl_toolkits.axes_grid1.inset_locator import mark_inset, inset_axes
plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True
x = np.linspace(0, 1, 100)
y = x ** 2
ax = plt.subplot(1, 1, 1)
ax.plot(x, y)
axins = inset_axes(ax, 1, 1, loc=2, bbox_to_anchor=(0.2, 0.55),
                    bbox_transform=ax.figure.transFigure)
axins.plot(x, y)
x1, x2 = .4, .6
y1, y2 = x1 ** 2, x2 ** 2
axins.set_xlim(x1, x2)
axins.set_ylim(y1, y2)
mark_inset(ax, axins, loc1=1, loc2=3, fc="none", ec="0.5")
plt.show()

Kết quả

Mục phóng to (zoomed inset) với tỷ lệ X, Y khác nhau trong Matplotlib