-1

I have a set of .txt named "occupancyGrid_i", i being a number from 0-100. What I'd like to do is to open every one of them and show them for 3 seconds. The data of the .txt is a [N x M] matrix.

import numpy
import matplotlib.pyplot as plt
import time

while True:

    matrix = numpy.loadtxt('res/matrix_' + str(i) + '.txt')

    plt.clf()
    plt.imshow(matrix)
    plt.show()
    time.sleep(3)
    i=i+1

What I have done so far doesn't seem to be enough. What am I doing wrong?

Latika Agarwal
  • 973
  • 1
  • 6
  • 11
Jorge
  • 27
  • 7
  • What do you mean by "isn't enough"? Not the expected output, not the expected behaviour, error messages? Apart from that, it is not clear, what you want to show and what your input is. – Mr. T Jun 17 '18 at 21:20
  • It isn't enough for my needs, i.e displaying in a loop every one of them for 3 seconds. What I get now with my code is just the first of the matrices, I'd have to close the window in order to visualize the next one. – Jorge Jun 18 '18 at 10:26

2 Answers2

1

You can try something like this, adapting the code suggested in this answer:

import os
import numpy as np
import pylab as plt

N_IMAGES = 100
VMIN, VMAX = 0, 1  # range of values in matrices

i = 0
while True:
    if i < N_IMAGES:
        path = 'res/matrix_' + str(i) + '.txt'
        if os.path.exists(path):  # check if file exists
            matrix = np.loadtxt('matrices/matrix_' + str(i) + '.txt')
            plt.imshow(matrix, vmin=VMIN, vmax=VMAX)
            plt.title("Matrix {}".format(i))
            plt.pause(3)
        i += 1
    else:
        # terminate you program or start from the beginning
        break
        # i = 0
        # continue
PieCot
  • 3,564
  • 1
  • 12
  • 20
0

I dont know what exactly your goal is. But to display text in matplotlib you can use text from pyplot.
`

import numpy
import matplotlib.pyplot as plt
import time
for i in range(1,5):
    s = ''
    with open(str(i)+'.txt','r') as f:
        s=f.read()
    plt.text(0.5, 0.67,s,transform=plt.gca().transAxes)
    plt.show()
    time.sleep(3)

First 2 argument (0.5 ,0.67) are cordinate of displayed text.
I think you should find some other way of displaying text. Just print them on your console, plotting them is not the best way to represent text data.

Poojan
  • 3,366
  • 2
  • 17
  • 33
  • What the matrix inside the .txt represents are occuppancy grids. I doesn't give me much information to see the numbers themselves, rather than visually giving me an idea of how it worked out. Thanks for your answer, it was truly helpful – Jorge Jun 18 '18 at 10:21