0

Escenario
I want to update the values of a heatmap with a time delay of 1 seconds. The objective is represent the evolution of the Q-Table in a reinforcement learning problem.

Error
The problem is that the heatmap figure is being updated but the values are kept intead of replaced. enter image description here

Code
Q is intially an all-zeros pandas DatraFrame
Function to create seaborn heatmap:

# Helper functions to draw, update and get values of the table
def draw_Table(Q):
    table = sns.heatmap(Q, cmap='Blues', annot=True, linewidths=.5, cbar=False, 
                linecolor='black', square=True).set_title('Q-Table')
    return table  

Here is the main functionality:

plt.ion()
plt.figure(figsize = (10,10))
for i in range(EPISODES):

    print('Episode [{}/{}]'.format(i,EPISODES))
    print('Current Q-Table')

    # Some code that updates the values of Q

    # Update the new Q-Value
    if 'previous' in globals(): del previous
    previous = draw_Table(Q)
    plt.pause(1)

plt.ioff()
plt.show()
ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712
Pablo Ruiz Ruiz
  • 605
  • 1
  • 6
  • 23
  • 1
    Looks like you are deleting the title in each iteration. Not sure if that is useful. Instead you would want to return the axes and `clear` (not delete) the axes. (Note that with a [mcve], I could also provide an answer.) – ImportanceOfBeingErnest Sep 07 '18 at 16:14

1 Answers1

0

Solution (according to comments) was found clearing the axes with:

# Update the new Q-Value
if 'previous' in globals(): 
    previous.axes.clear()
previous = draw_Table(Q)

[This was edited out of the question into an answer]

ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712