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?
1 Answer
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()
2 Comments
DuttaA
Can you explain the expand() and the aspect 100 part?
dennlinger
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.