-1

I have values in a 2D array which I ploy them using the following lines:

t1 = [[self.ax.text(i+.2,n-.6-j,myArray[0][j][i])
        for i in range(myArray.shape[2])]
        for j in range(myArray.shape[1])]

and I'm trying to update them using this:

t1.set_text(myArray[self.ref][0][0])

The error is:

AttributeError: 'list' object has no attribute 'set_text'

However, which I try to update one value only, like:

    t1 = self.ax.text(10,10, myArray[0][0][0])

it works just fine.
My questions is: How can I update the plotting of the values of the whole array at once? And by update I mean removing the previous values and plotting the new once so they don't pile up.

Addition

I tried applied the solution mentioned here:
How to refresh text in Matplotlib?
and extend it to my problem, still didn't work.

philippos
  • 1,142
  • 5
  • 20
  • 41
  • `t1` in python's list, not plot, and it doesn't have `set_text()`. With `t1 = self.ax.text()` you replace list with different object and you can do the same wiithout `t1 =` – furas Jan 08 '18 at 07:22

1 Answers1

1

t1 is 2D list with many text elements and you can do the same with something like

t1 = []

for j in range(myArray.shape[1]):
    sublist = []        
    for i in range(myArray.shape[2]):
       txt = self.ax.text(i+.2,n-.6-j,myArray[0][j][i])
       sublist.append(txt)
    t1.append(sublist)

So you can do something similar with set_text() to change text

for j in range(myArray.shape[1]):
    for i in range(myArray.shape[2]):
       t1[i][j].set_text(myArray[0][j][i])

or using enumerate()

for j, row in enumerate(t1):
    for i, cell in enumerate(row):
       cell.set_text(myArray[0][j][i])

or using zip()

for row, arr_row in zi(t1, myArray[0]):
    for cell, arr_cell in zip(row, arr_row):
       cell.set_text(arr_cell)

BTW: all examples are not tested so they may need some changes.


Using t1 = self.ax.text(10,10, myArray[0][0][0]) you replaced list with single element so you loose access to all elements.

furas
  • 134,197
  • 12
  • 106
  • 148
  • Thanks, I tried the first solution and it worked well. I have another question I don't know if I shall post in a separate question. I want to remove the added text using a button with one click. I have been trying with something like `t1.remove()`, however it's not working. Could you help me please in this issue as well? – philippos Jan 08 '18 at 09:07
  • 1
    `t1` is python list, not text on `plot` - and it has method `remove()` which removes item from list (which is only reference to text item on `plot)`, not item from `plot`. To remove text from plot you have to get element from list and use matplotlib method to remove it - `t1[0][0].remove()` . For more elements you have to use `for` loops. – furas Jan 08 '18 at 09:14