1

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:

enter image description here

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'
Mateusz Konopelski
  • 1,000
  • 4
  • 20
  • 37
  • `imshow` will treat the array values as pixels in an image, not text formatted in decimal notation. You can look at LaTeX as a means to getting your text formatted. Matplotlib will have some utilitites for that. – Mad Physicist Feb 08 '19 at 16:38
  • @MadPhysicist was right i misunderstood the question. My previous comment that he is referring to has been deleted not to confuse others. I have found this [git post](https://gist.github.com/kylemcdonald/2f1b9a255993bf9b2629) that looks promissing – Edeki Okoh Feb 08 '19 at 16:51
  • Your edit is good. You would want to look into the `plt.text` function for creating a text object. Text objects can understand latex. You probably wouldn't need anything outside the \begin/end{bmatrix} tags. – Mad Physicist Feb 08 '19 at 17:01
  • I am running my python on kaggle kernel and there is some problem with matplotlib package because even code copied straight from matplotlib website gives me this error: `FileNotFoundError: [Errno 2] No such file or directory: 'latex': 'latex'` – Mateusz Konopelski Feb 08 '19 at 17:13

2 Answers2

1

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:

Matrix

jdehesa
  • 58,456
  • 7
  • 77
  • 121
  • I tested it on local computer and received errorŁ `ValueError: \left[\begin{array}{ccccc}1 & \quad & 2 & \quad & 3 \\ 4 & \quad & 5 & \quad & 6 \\ 7 & \quad & 8 & \quad & 9\end{array}\right] ^ Expected "\right" (at char 6), (line:1, col:7)` – Mateusz Konopelski Feb 08 '19 at 17:41
  • @MateuszKonopelski Have you tried to produce any other plot with LaTeX? It may be that you need to configure your environment? – jdehesa Feb 08 '19 at 20:58
  • I've contacted product support on Kaggle and they acknowledged that their machine doesn't have latex. When it comes to mind - I will test it again. Anyway - your anwser was exactly what I was looking for so I will mark it. Thanks!. – Mateusz Konopelski Feb 11 '19 at 12:53
0

It seems like the Kaggle kernel you are using is not able to access a LaTex interpreter.

If you just want to create a few images for a presentation you can check out Latex 2 png. Here you have to enter

\begin{bmatrix}
    1 & 2 & 3 \\
    4 & 5 & 6 \\
    7 & 8 & 9

  \end{bmatrix}

which will result in the following image:

Image created with latex2png.com

T. Kau
  • 593
  • 1
  • 4
  • 17