13

I am trying to use suptitle to print a title, and I want to occationally replace this title. Currently I am using:

self.ui.canvas1.figure.suptitle(title)

where figure is a matplotlib figure (canvas1 is an mplCanvas, but that is not relevant) and title is a python string.

Currently, this works, except for the fact that when I run this code again later, it just prints the new text on top of the old, resulting in a gargeled, unreadable title.

How do you replace the old suptitle of a figure, instead of just printing over?

Thanks,

Tyler

3 Answers 3

19

figure.suptitle returns a matplotlib.text.Text instance. You can save it and set the new title:

txt = fig.suptitle('A test title')
txt.set_text('A better title')
plt.draw() 
2
  • This didn't work...still does the same bug where it just renders the new text over the old. May 14, 2012 at 20:14
  • Ah, I more or less did this. I was working with mpl_canvases. Use this, except save the self.mpl_canvas_obj.figure.suptitle('') somewhere, and then use that as the txt field. May 14, 2012 at 20:49
8

Resurrecting this old thread because I recently ran into this. There is a references to the Text object returned by the original setting of suptitle in figure.texts. You can use this to change the original until this is fixed in matplotlib.

0

I had similar problem. Method suptitile of figure object show title over old title (previously created). This is definately a bug in matplotlib. Especially as you can find this code in figure.py (part of matplotlib package):

        (...)

        sup = self.text(x, y, t, **kwargs)
        if self._suptitle is not None:
            self._suptitle.set_text(t)
            self._suptitle.set_position((x, y))
            self._suptitle.update_from(sup)
        else:
            self._suptitle = sup
        return self._suptitle

Luckily, this bug is present in matplotlib version 1.2.1 but it was later fixed (in 2.2.4, it is no longer present). Try update of matplotlib, it will fix it for you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.