缩放由 imshow 生成的图像
Scaling images generated by imshow
以下代码片段
import matplotlib.pyplot as plt
import numpy as np
arr1 = np.arange(100).reshape((10,10))
arr2 = np.arange(25).reshape((5,5))
fig, (ax1, ax2, ) = plt.subplots(nrows=2, figsize=(3,5))
ax1.imshow(arr1, interpolation="none")
ax2.imshow(arr2, interpolation="none")
plt.tight_layout()
plt.show()
生成两个大小相同的图像,但第二个 "pixel density" 小得多。
我想让第二张图片以与第一张相同的比例(即像素密度)绘制,不填充子图,可能正确对齐(即图像的原点与第一张相同的子图位置一个。)
编辑
arr1
和arr2
的形状只是一个例子来说明问题。我正在寻找的是一种确保 imshow
在图中不同部分生成的两个不同图像以完全相同的比例绘制的方法。
我能想到的最简单的方法都行不通,但 gridspec
行。这里的原点没有明确对齐,它只是利用了 gridspec 填充行的方式(并且有一个未使用的子图作为间隔)。
import matplotlib.pyplot as plt
import numpy as np
from matplotlib import gridspec
sizes = (10, 5)
arr1 = np.arange(sizes[0]*sizes[0]).reshape((sizes[0],sizes[0]))
arr2 = np.arange(sizes[1]*sizes[1]).reshape((sizes[1],sizes[1]))
# Maybe sharex, sharey? No, we pad one and lose data in the other
#fig, (ax1, ax2, ) = plt.subplots(nrows=2, figsize=(3,5), sharex=True, sharey=True)
fig = plt.figure(figsize=(3,5))
# wspace so the unused lower-right subplot doesn't squeeze lower-left
gs = gridspec.GridSpec(2, 2, height_ratios = [sizes[0], sizes[1]], wspace = 0.0)
ax1 = plt.subplot(gs[0,:])
ax2 = plt.subplot(gs[1,0])
ax1.imshow(arr1, interpolation="none")
ax2.imshow(arr2, interpolation="none")
plt.tight_layout()
plt.show()
以下代码片段
import matplotlib.pyplot as plt
import numpy as np
arr1 = np.arange(100).reshape((10,10))
arr2 = np.arange(25).reshape((5,5))
fig, (ax1, ax2, ) = plt.subplots(nrows=2, figsize=(3,5))
ax1.imshow(arr1, interpolation="none")
ax2.imshow(arr2, interpolation="none")
plt.tight_layout()
plt.show()
生成两个大小相同的图像,但第二个 "pixel density" 小得多。
我想让第二张图片以与第一张相同的比例(即像素密度)绘制,不填充子图,可能正确对齐(即图像的原点与第一张相同的子图位置一个。)
编辑
arr1
和arr2
的形状只是一个例子来说明问题。我正在寻找的是一种确保 imshow
在图中不同部分生成的两个不同图像以完全相同的比例绘制的方法。
我能想到的最简单的方法都行不通,但 gridspec
行。这里的原点没有明确对齐,它只是利用了 gridspec 填充行的方式(并且有一个未使用的子图作为间隔)。
import matplotlib.pyplot as plt
import numpy as np
from matplotlib import gridspec
sizes = (10, 5)
arr1 = np.arange(sizes[0]*sizes[0]).reshape((sizes[0],sizes[0]))
arr2 = np.arange(sizes[1]*sizes[1]).reshape((sizes[1],sizes[1]))
# Maybe sharex, sharey? No, we pad one and lose data in the other
#fig, (ax1, ax2, ) = plt.subplots(nrows=2, figsize=(3,5), sharex=True, sharey=True)
fig = plt.figure(figsize=(3,5))
# wspace so the unused lower-right subplot doesn't squeeze lower-left
gs = gridspec.GridSpec(2, 2, height_ratios = [sizes[0], sizes[1]], wspace = 0.0)
ax1 = plt.subplot(gs[0,:])
ax2 = plt.subplot(gs[1,0])
ax1.imshow(arr1, interpolation="none")
ax2.imshow(arr2, interpolation="none")
plt.tight_layout()
plt.show()