0

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

1 Answer 1

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()

Sign up to request clarification or add additional context in comments.

2 Comments

Can you explain the expand() and the aspect 100 part?
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.

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.