[英]Place multiple numpy arrays into a larger array
I wish to create a large array and replace some of the values with two other arrays. Each assignment works independently but the second statement overrides the first.我希望创建一个大数组并用另外两个 arrays 替换一些值。每个赋值独立工作,但第二个语句覆盖第一个。 I wish to see both images in the background plot.
我希望在背景中看到这两个图像 plot。
import numpy as np
import matplotlib.pyplot as plt
#initialize the arrays
img1 = np.random.rand(400,400)
img2 = np.arange(0,10000).reshape((100,100))
background = np.zeros(shape = (1600, 1600))
#embed the background with the 1st image
background[0:img1.shape[0], 0:img1.shape[1]] = img1
#place the other image somewhere else in the array
background[500:(100+500), 500:(100+500)] = img2
#plot the results
fig, ax = plt.subplots(3, 1, figsize = (5,5))
ax[0].imshow(img1, cmap = 'rainbow')
ax[1].imshow(img2, cmap = 'rainbow')
ax[2].imshow(background, cmap = 'rainbow')
plt.show()
The problem is not that "the second statement overrides the first".问题不在于“第二个语句覆盖第一个”。 The problem (ie the reason that you're only seeing one of the images within the composite image) is that the entries in image 2 are much larger than those of image 1. When you show img1 alone, the colormap fits the colors over a scale from 0 to (approximately) 1. When the two images are shown together, the colors are scaled from 0 to 10000, so the values from img1 are all too close to zero for the image to show up.
问题(即您只看到合成图像中的一个图像的原因)是图像 2 中的条目比图像 1 中的条目大得多。当您单独显示 img1 时,颜色图适合 colors从 0 缩放到(大约)1。当两个图像一起显示时,colors 从 0 缩放到 10000,因此 img1 的值都太接近零,图像无法显示。
Here's a change to your code that scales the values of the second array down to a maximum value of 1;这是对代码的更改,将第二个数组的值缩小到最大值 1; note that both images show up this time.
请注意,这两个图像都出现了。
import numpy as np
import matplotlib.pyplot as plt
#initialize the arrays
img1 = np.random.rand(400,400)
img2 = np.arange(0,10_000).reshape((100,100))/10_000
background = np.zeros(shape = (800, 800))
#embed the background with the 1st image
background[0:img1.shape[0], 0:img1.shape[1]] = img1
#place the other image somewhere else in the array
background[500:(100+500), 500:(100+500)] = img2
#plot the results
fig, ax = plt.subplots(3, 1, figsize = (5,5))
ax[0].imshow(img1, cmap = 'rainbow')
ax[1].imshow(img2, cmap = 'rainbow')
ax[2].imshow(background, cmap = 'rainbow')
plt.show()
The result:结果:
Incidentally, if you want to combine arrays on a zero "background" in this manner, you might want to consider using the scipy.linalg.block_diag
method.顺便说一句,如果您想以这种方式在零“背景”上组合 arrays,您可能需要考虑使用
scipy.linalg.block_diag
方法。 For instance,例如,
import numpy as np
import matplotlib.pyplot as plt
from scipy.linalg import block_diag
#initialize the arrays
img1 = np.random.rand(400,400)
img2 = np.arange(0,10_000).reshape((100,100))/10_000
space1 = np.zeros([100,100])
space2 = np.zeros([100,100])
composite = block_diag(img1, space1, img2, space2)
#plot the results
fig, ax = plt.subplots(3, 1, figsize = (5,5))
ax[0].imshow(img1, cmap = 'rainbow')
ax[1].imshow(img2, cmap = 'rainbow')
ax[2].imshow(composite, cmap = 'rainbow')
plt.show()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.