2

I have an array A with shape (3,3). Is there a way to represent the array elements on a square of size 3x3? In general, I would like to represent nxn arrays on a square of size nxn? The expected output is attached.

import numpy as np

A=np.array([[10,20,30],[40,50,60],[70,80,90]])

The expected output is:

Output

4
  • Just print(A)? Commented Jun 28, 2022 at 15:14
  • Do you want an image or text? Commented Jun 28, 2022 at 15:15
  • I want an image with array elements mentioned in the above format. Commented Jun 28, 2022 at 15:17
  • 4
    If you want an image you can go with matplotlib annotated heatmaps. Commented Jun 28, 2022 at 15:18

1 Answer 1

2

You could use seaborn.heatmap that has a nice API:

import numpy as np
import seaborn as sns
from matplotlib.colors import ListedColormap

A = np.array([[10, 20, 30], [40, 50, 60], [70, 80, 90]])

ax = sns.heatmap(A,
            annot=True, square=True, cbar=False,
            xticklabels=False, yticklabels=False,
            cmap=ListedColormap(['white']),
            linecolor='k', lw=2,
            annot_kws={'size': 30}
           )

ax.figure.savefig('img.png')

output:

enter image description here

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

Comments

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.