简体   繁体   English

将多个 numpy arrays 放入一个更大的数组

[英]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.

相关问题 使用 Numpy 将图像作为数组添加到更大的数组中 - Adding images as arrays into a larger array using Numpy (新数组名称中的可迭代对象)使用 for 循环创建多个具有不同名称的 numpy 数组以打破更大的 numpy 数组 - (iterable object in the new array name) Creating multiple numpy arrays with different names using a for loop to break a larger numpy array numpy:如何将两个排序数组合并为一个更大的排序数组? - numpy: How to merge two sorted arrays into a larger sorted array? 是否可以对多个numpy数组执行相同的随机播放? - Is it possible to perform the same shuffle on multiple numpy arrays in place? 使用自定义dtype就地替换numpy数组值 - replacing numpy array values in-place for arrays with custom dtype 1 个阵列中的多个 2D numpy arrays - Multiple 2D numpy arrays in 1 array 在一个数组中的多个数组中存储多个数组Python / Numpy - Storing multiple arrays within multiple arrays within an array Python/Numpy Numpy 数组 numpy arrays - Numpy array of numpy arrays 根据两个 numpy 数组的多个条件创建 numpy 数组 - Create numpy array based on multiple conditions on two numpy arrays 将包含多个 numpy arrays 的列表转换为单个 numpy 数组 - Convert list containing multiple numpy arrays to single numpy array
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM