以前的图使用matplotlib交互式与新图重叠

问题描述 投票:0回答:1

Escenario 我想更新时间延迟为1秒的热图值。目标是代表强化学习问题中Q表的演变。

错误 问题是热图数字正在更新,但值保持被替换。 enter image description here

码 Q最初是一个全零的pandas DataFrame 创建seaborn热图的功能:

# 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  

这是主要功能:

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()
matplotlib interactive python-interactive
1个回答
0
投票

发现解决方案(根据评论)清除轴:

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

[这被编辑成问题的答案]

© www.soinside.com 2019 - 2024. All rights reserved.