Text 保存期间隐藏Matplotlib文本

Text 保存期间隐藏Matplotlib文本,text,matplotlib,Text,Matplotlib,我有一个“暂停”Matplotlib文本,告诉用户图形已暂停。这非常有效,但我不希望在打印或保存时显示“暂停”一词 figPausedText = fig.text(0.5, 0.5,'Paused', horizontalalignment='center', verticalalignment='center', transform=ax.transAxes, alpha = 0.25, size='x-large') 保存/打印时隐藏暂停文本的最佳方法是什

我有一个“暂停”Matplotlib文本,告诉用户图形已暂停。这非常有效,但我不希望在打印或保存时显示“暂停”一词

figPausedText = fig.text(0.5, 0.5,'Paused', horizontalalignment='center',
    verticalalignment='center',
    transform=ax.transAxes,
    alpha = 0.25,
    size='x-large')
保存/打印时隐藏暂停文本的最佳方法是什么?如果我可以绑定到所有保存和打印命令,我很乐意设置文本(“”)。我特别想确保当用户单击NavigationToolbar2TkAgg工具栏时,它能正常工作。

类似于以下内容:

figPausedText = fig.text(...)
def my_save(fig, * args, **kwargs):
     figPausedText.set_visible(False)
     fig.savefig(*args, **kwargs)
     figPausedText.set_visible(True)
如果你想变得更聪明,你可以用猴子修补你的
图形
对象:

import types

figPausedText = fig.text(...)
# make sure we have a copy of the origanal savefig
old_save = matplotlib.figure.Figure.savefig
# our new function which well hide and then re-show your text
def my_save(fig, *args, **kwargs):
     figPausedText.set_visible(False)
     ret = old_save(fig, *args, **kwargs)
     figPausedText.set_visible(True)
     return ret
# monkey patch just this instantiation  
fig.savefig = types.MethodType(my_save, fig)
或者,如果需要通过工具栏执行此操作

import types

figPausedText = fig.text(...)
# make sure we have a copy of the origanal print_figure
old_print = fig.canvas.print_figure # this is a bound function
# if we want to do this right it is backend dependent
# our new function which well hide and then re-show your text
def my_save(canvas, *args, **kwargs):
     figPausedText.set_visible(False)
     ret = old_print(*args, **kwargs) # we saved the bound function, so don't need canvas
     figPausedText.set_visible(True)
     return ret
# monkey patch just this instantiation  
fig.canvas.print_figure = types.MethodType(my_save, fig.canvas)
大概是这样的:

figPausedText = fig.text(...)
def my_save(fig, * args, **kwargs):
     figPausedText.set_visible(False)
     fig.savefig(*args, **kwargs)
     figPausedText.set_visible(True)
如果你想变得更聪明,你可以用猴子修补你的
图形
对象:

import types

figPausedText = fig.text(...)
# make sure we have a copy of the origanal savefig
old_save = matplotlib.figure.Figure.savefig
# our new function which well hide and then re-show your text
def my_save(fig, *args, **kwargs):
     figPausedText.set_visible(False)
     ret = old_save(fig, *args, **kwargs)
     figPausedText.set_visible(True)
     return ret
# monkey patch just this instantiation  
fig.savefig = types.MethodType(my_save, fig)
或者,如果需要通过工具栏执行此操作

import types

figPausedText = fig.text(...)
# make sure we have a copy of the origanal print_figure
old_print = fig.canvas.print_figure # this is a bound function
# if we want to do this right it is backend dependent
# our new function which well hide and then re-show your text
def my_save(canvas, *args, **kwargs):
     figPausedText.set_visible(False)
     ret = old_print(*args, **kwargs) # we saved the bound function, so don't need canvas
     figPausedText.set_visible(True)
     return ret
# monkey patch just this instantiation  
fig.canvas.print_figure = types.MethodType(my_save, fig.canvas)

这看起来不错,但不幸的是,单击NavigationToolbar2TkAgg保存按钮时未调用my_保存。这如何与NavigationToolbar2TkAgg一起工作?我尝试了当时的monkey补丁
画布上的变体。打印\u figure
而不是通过gui工具栏编辑您的问题,以表明您希望此操作正常。这看起来不错,但不幸的是,单击NavigationToolbar2TkAgg save按钮时未调用my\u save。这如何与NavigationToolbar2TkAgg一起工作?我尝试了当时的monkey patch
画布上的变体。请打印图,而不是编辑您的问题,以表明您希望通过gui工具栏实现此功能。