1

I have a 'Paused' Matplotlib text to tell the user the graph is paused. This works great, but I do not want the word "paused" to show when printing or saving.

figPausedText = fig.text(0.5, 0.5,'Paused', horizontalalignment='center',
    verticalalignment='center',
    transform=ax.transAxes,
    alpha = 0.25,
    size='x-large')

What's the best way to hide the Paused text when saving/printing? I'm happy to set_text('') if I can bind to all save and print commands. I particularly want to make sure it works when a user clicks the NavigationToolbar2TkAgg toolbar.

0

1 Answer 1

1

Something like this:

figPausedText = fig.text(...)
def my_save(fig, * args, **kwargs):
     figPausedText.set_visible(False)
     fig.savefig(*args, **kwargs)
     figPausedText.set_visible(True)

If you want to get really clever, you can monkey patch your Figure object:

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)

or if you need this to work through the tool bar

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)
Sign up to request clarification or add additional context in comments.

3 Comments

This looks good, but unfortunately the my_save was not called when the NavigationToolbar2TkAgg save button was clicked. How can this work with the NavigationToolbar2TkAgg? I tried variants from the saving documentation
Then monkey patch canvas.print_figure instead
Also, edit your question to indicate that you want this to work through the gui tool bar.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.