So I have got a string of characters and I am representing it by a number between 1-5 in a numpy array. Now I want to convert it to a pictorial form by first repeating the string of numbers downwards so the picture becomes broad enough to be visible (since single string will give a thin line of picture). My main problem is how do I convert the array of numbers to a picture?
Asked
Active
Viewed 88 times
0
-
Possible duplicate of [Imshow: extent and aspect](https://stackoverflow.com/questions/13384653/imshow-extent-and-aspect) – dennlinger Jul 02 '18 at 05:56
1 Answers
1
This would be a minimal working example to visualize with matplotlib:
import numpy as np
import matplotlib.pyplot as plt
# generate 256 by 1 vector of values 1-5
img = np.random.randint(1,6, 256)
# transpose for visualization
img = np.expand_dims(img, 1).T
# force aspect ratio
plt.imshow(img, aspect=100)
# or, alternatively use aspect='auto'
plt.show()
You can force the aspect ratio of the plotted figure by simply setting the aspect
option of imshow()

dennlinger
- 9,890
- 1
- 42
- 63
-
-
the `expand_dims` simply forces the numpy array to be two-dimensional; per default, the `img` array is 1D (of shape (256,)), but expand dims forces it to be two-dimensional. And a scalar value for aspect simply sets the aspect ratio (either width/height or height/width) to whatever you provide. – dennlinger Jul 02 '18 at 08:12