像这样的东西:
figPausedText = fig.text(...)
def my_save(fig, * args, **kwargs):
figPausedText.set_visible(False)
fig.savefig(*args, **kwargs)
figPausedText.set_visible(True)
如果你想变得非常聪明,你可以猴子修补你的Figure
对象:
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)