1

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?

3 Answers 3

2

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')
Sign up to request clarification or add additional context in comments.

4 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.
0

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)

3 Comments

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
0

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

2 Comments

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.