ลบขอบสีขาว (White Border) รอบภาพ imshow โดยตั้งค่า Figure Size ให้ตรง Aspect Ratio ของข้อมูล และปิด Axis
ตัวอย่างโค้ด
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()
คำอธิบาย
fig.set_size_inches(width, height)ตั้งขนาด Figure (Inches)- Width/Height Ratio = Data Shape Ratio (50/50 = 1) -> Square
plt.Axes(fig, [left, bottom, width, height])สร้าง Axes ครอบเต็ม Figure [0,0,1,1]ax.set_axis_off()ปิด Axis ทั้งหมด (Ticks, Labels, Spines)fig.add_axes(ax)เพิ่ม Axes เข้า Figure- วิธีง่ายกว่า:
plt.imshow(data); plt.axis('off'); plt.subplots_adjust(0,0,1,1)