## How to insert value in numpy ndArray?

I have two ndArray.

ex: x = np.array([110,200, 500,100]) y = np.array([50,150,30,70])

Now based on their value I have created an image.

x_shape = np.max(x) #x_shape=500 y_shape = np.max(y) #y-shape=150 image = np.zeros((x_shape+1, y_shape+1))

according to my data now my image size is (501,151)

Now, How can I insert data from (x, y) as x,y pair? I mean for the pixel value: (110,50), (200,150), (500,30), (100,70) I want the image will be white and the rest pixel will be dark. How can I achieve this?

Based on OP's own answer, one can improve it by using a vectorized approach:

import numpy as np import matplotlib.pyplot as plt x = np.array([110,200, 500,100]) y = np.array([50,150,30,70]) x = np.floor(x / 10).astype(int) y = np.floor(y / 10).astype(int) x_shape = np.max(x) # x_shape = 500 y_shape = np.max(y) # y_shape = 150 image = np.zeros((x_shape + 10, y_shape + 10)) image[x, y] = 10 plt.imshow(image)

(To be fair, I did not understand from the question that this is what OP was after).

##### EDIT

To address the "visualization issue" without resizing from the comments:

import numpy as np import matplotlib.pyplot as plt x = np.array([110, 200, 500, 100]) y = np.array([50, 150, 30, 70]) x_shape = np.max(x) y_shape = np.max(y) image = np.zeros((x_shape + 1, y_shape + 1)) image[x, y] = 10 plt.figure(figsize=(20, 20)) plt.imshow(image.transpose(), interpolation='nearest', aspect='equal')

**numpy.insert — NumPy v1.19 Manual,** You can use numpy.insert , though unlike list.insert it returns a new array because arrays in NumPy have fixed size. >>> import numpy as np� array1: Numpy Array, original array array2: Numpy Array, To Append the original array. axis: It is optional default is 0. Axis along which values are appended. Here axis is not passed as an argument so, elements will append with the original array a, at the end.

Well, I got the answer. It was easy and as I am new it makes me confused.

import numpy as np import matplotlib.pyplot as plt x = np.array([110,200, 500,100]) y = np.array([50,150,30,70]) x = np.floor(x/10).astype(int) #devided by 10 to reduce the img size y = np.floor(y/10).astype(int) #devided by 10 to reduce the img size x_shape = np.max(x) #x_shape=500 y_shape = np.max(y) #y-shape=150 image = np.zeros((x_shape+10, y_shape+10)) for x, y in zip(x,y): image[x,y]=200 plt.imshow(image)

**Insert element into numpy array,** The insert() function is used to insert values along the given axis before the given indices. Input array. Object that defines the index or indices before which values is inserted. Support for multiple insertions when obj is a single scalar or a sequence with one element (similar to calling insert multiple times). numpy.insert¶ numpy.insert (arr, obj, values, axis=None) [source] ¶ Insert values along the given axis before the given indices. Parameters arr array_like. Input array. obj int, slice or sequence of ints. Object that defines the index or indices before which values is inserted.

not sure exactly what do you need you may try

a = np.array([110, 200, 500, 100]) b = np.array([50, 150, 30, 70]) np.array([zip(x,y) for x,y in zip(a,b)]) pd.DataFrame(list(zipped))``` ##or another representation np.dstack((x,y)) both are taken from https://stackoverflow.com/questions/49461605/why-do-we-need-to-convert-a-zipped-object-into-a-list [1]: https://stackoverflow.com/questions/49461605/why-do-we-need-to-convert-a-zipped-object-into-a-list

**NumPy Array manipulation: insert() function,** In this tutorial, you'll learn how to perform many Python NumPy array operations such as adding, deleting, sorting, and extracting values, row,� Add array element. You can add a NumPy array element by using the append() method of the NumPy module. The syntax of append is as follows: numpy.append(array, value, axis) The values will be appended at the end of the array and a new ndarray will be returned with new and old values as shown above.

**Python NumPy array tutorial,** insert(array, object, values, axis = None) : inserts values along the mentioned axis before the given indices. Parameters : array : [array_like]Input� numpy.append¶ numpy.append (arr, values, axis=None) [source] ¶ Append values to the end of an array. Parameters arr array_like. Values are appended to a copy of this array. values array_like. These values are appended to a copy of arr. It must be of the correct shape (the same shape as arr, excluding axis).

**numpy.insert() in Python,** Input array. obj : int, slice or sequence of ints. Object that defines the index or indices before which values is inserted. Return value: out [ndarray] A copy of arr with values inserted. Note that insert does not occur in-place: a new array is returned. If axis is None, out is a flattened array.

**numpy.insert — NumPy v1.9 Manual,** numpy.ndarray.put�. ndarray. put (indices, values, mode='raise')�. Set a.flat[n] = values[n] for all n in indices. Refer to numpy.put for full documentation. See also. The values of a ndarray are stored in a buffer which can be thought of as a contiguous block of memory bytes. So how these bytes will be interpreted is given by the dtype object. Every Numpy array is a table of elements (usually numbers), all of the same type, indexed by a tuple of positive integers.

##### Comments

- This is called fancy indexing by the way
- thanks for the answer. But if we dont device by 10 then its not visible. I guess singel pixel is too small to see. Can you suggest any modification without devide by 10? @norok2
- answer accepted and upvorted. thanks. Will be glad if you give me the modified version. @norok2
- @Kazi that is a visualization, issue.. take a look here. I have also included an edit to address this.
- This approach is not very efficient. You can achieve much better performances with a vectorized code.
- I have no idea about vectorized. can you create an answer. I might accept if it works. thanks. @norok2
- done... basically you just take advantage of advanced indexing
- thanks for the answer. it helped to find the solution. actually i want to create a image from some values which is in cartesian coordinates. anyway upvoted for your help @olena
- This is not a good way. You are discarding all the benefits of numpy, and don't specify what
`a`

and`b`

are supposed to be.