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

Python Matplotlib - Loại bỏ viền trắng khi dùng subplot và imshow


Tạo figure có kích thước chính xác bằng tỷ lệ ảnh, thêm axes chiếm toàn bộ figure ([0,0,1,1]), tắt axis, sau đó vẽ imshow() để hiển thị ảnh không viền.

Ví dụ

import numpy as np
import matplotlib.pyplot as plt

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

data = np.random.randint(0, 50, (50, 50))
sizes = np.shape(data)
fig = plt.figure()
fig.set_size_inches(1. * sizes[0] / sizes[1], 1, forward=False)
ax = plt.Axes(fig, [0., 0., 1., 1.])
ax.set_axis_off()
fig.add_axes(ax)
ax.imshow(data)
plt.show()

Kết quả

Hiển thị ảnh 50x50 đầy màn hình figure, không có viền trắng, không có trục.