Visualize 3×3 numpy array and save it as new array of shape: 400×600

  • A+
Category:Languages

I have array like:

import numpy as np np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) 

and I want to visualize in some nice way, like:

Visualize 3x3 numpy array and save it as new array of shape: 400x600

but having resolution: 400 x 600.

So in the end I want to have new array of shape (400, 600) which if plotted using:

import matplotlib.pyplot as plt plt.imshow(my_new_array) 

will show image like above.

Is it possible? Is there some package that help me?

[edit:] After @Mad Physicist comment, I researched latex and following code would do the job:

/documentclass{article} /usepackage{amsmath} /begin{document}     /[       /begin{bmatrix}     1 & 2 & 3 //     4 & 5 & 6 //     7 & 8 & 9    /end{bmatrix} /]  /end{document} 

and matplotlib has option to use text on axis and title but I would not know how to use it in body of plot. Maybe empty plot and use title in the middle?

[edit2]: Second answer here is the closest to what I need: Converting latex code to Images (or other displayble format) with Python However, as stated in my comment below I am running into error on kaggle with different methods which uses matplotlib and latex:

FileNotFoundError: [Errno 2] No such file or directory: 'latex': 'latex' 

 


Matplotlib can render text with LaTeX, so you can take advantage of that to make this kind of visualization:

import numpy as np import matplotlib.pyplot as plt  def show_mat(a, font_size, resolution=None, dpi=None):     resolution = resolution or (600, 400)     dpi = dpi or 100     res_x, res_y = resolution     inc_x = res_x / dpi     inc_y = res_y / dpi     rows, cols = a.shape     fig = plt.figure(figsize=(inc_x, inc_y), dpi=dpi)     ax = fig.add_subplot(111)     ax.set_axis_off()     a_str = r' // '.join(' & /quad & '.join(map(str, row)) for row in a)     alig = 'c' * (2 * cols - 1)     tex = r"$/left[/begin{{array}}{{{}}}{}/end{{array}}/right]$".format(alig, a_str)     ax.text(0.5, 0.5, tex, size=font_size,             horizontalalignment='center', verticalalignment='center',             transform=ax.transAxes)  show_mat(np.arange(1, 10).reshape(3, 3), font_size=50) 

Output:

Visualize 3x3 numpy array and save it as new array of shape: 400x600

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: