Issue
x = np.array[[[8, 7, 1, 0, 3],
[2, 8, 5, 5, 2],
[1, 1, 1, 1, 1]],
[[8, 4, 1, 0, 0],
[6, 8, 5, 5, 2],
[1, 1, 1, 1, 1]],
[[2, 4, 0, 2, 3],
[2, 5, 5, 3, 2],
[1, 1, 1, 1, 1]],
[[4, 7, 2, 8, 0],
[1, 3, 6, 5, 2],
[1, 1, 1, 1, 1]]]
I want to get an image from this NumPy
file with the imshow()
command of the matplotlib
library but I shouldn't convert it to 3d array. Is there any way to read 3D array and plot it as 2D like this picture.
If I couldn't explain, I normally need to convert the array we have to 2D and draw it. Normally I can use commands like np.concatenate
or np.append
for this and get the following array.
x = np.array[[[8, 7, 1, 0, 3, 8, 4, 1, 0, 0, 2, 4, 0, 2, 3, 4, 7, 2, 8, 0],
[2, 8, 5, 5, 2, 6, 8, 5, 5, 2, 2, 5, 5, 3, 2, 1, 3, 6, 5, 2],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]
But I need to draw it without converting it to 2D.
Solution
Your image
depicts a 3 rows × 20 columns array and you start with a (4,3,5)
shape, and from the 2D array you've printed
x = np.array([[8, 7, 1, 0, 3, 8, 4, 1, 0, 0, 2, 4, 0, 2, 3, 4, 7, 2, 8, 0],
[2, 8, 5, 5, 2, 6, 8, 5, 5, 2, 2, 5, 5, 3, 2, 1, 3, 6, 5, 2],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]])
I see that you need to concatenate 4 matrices with 3 rows and 5 columns, hence the last dimension must stay untouched while the first two axes must be swapped, so I think the right incantation would be
...
plt.imshow(np.transpose(x, (1,0,2)).reshape(3,20))
...
that gives me
Answered By - gboffi
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.