📜  如何在 Matplotlib 中更改 imshow 纵横比?

📅  最后修改于: 2022-05-13 01:54:54.152000             🧑  作者: Mango

如何在 Matplotlib 中更改 imshow 纵横比?

纵横比是我们要显示的图像的高宽比。 Matplotlib 通过指定图像绘图的可选纵横比属性的值,为我们提供了修改图像纵横比的功能。

我们可以用“equal”、“auto”或任何表示所需纵横比的浮点值替换纵横比的值。在本文中,我们使用下图来演示使用 Matplotlib 的纵横比变化。

原图

例子:

Python3
import matplotlib.pyplot as plt
import cv2
  
# reading image from directory
im = cv2.imread("C://Users/User/Downloads/chess5.png")
  
# plotting a figure for showing all 
# images in a single plot
fig = plt.figure(figsize=(4, 4))
  
# plotting each matplot image with
# different aspect ratio parameter values
# in a seperate subplot
ax1 = fig.add_subplot(2, 2, 1)
ax1.set_xlabel('Original')
  
# plot the initial image as the first image
plt.imshow(im)
  
ax2 = fig.add_subplot(2, 2, 2)
ax2.set_xlabel('Aspect Ratio : Auto')
  
# plot the image with "auto" aspect ratio
# as the second image
plt.imshow(im, aspect='auto')
  
ax3 = fig.add_subplot(2, 2, 3)
ax3.set_xlabel('Aspect Ratio : 0.5')
  
# plot the image with "0.5" aspect ratio 
# as the third image
plt.imshow(im, aspect='0.5')
  
ax4 = fig.add_subplot(2, 2, 4)
ax4.set_xlabel('Aspect Ratio : 1.5')
  
# plot the image with "1.5" aspect ratio
# as the fourth image
plt.imshow(im, aspect='1.5')
  
# display the plot
plt.show()


输出: