7

The default window title of a figure is figure X, where X is increased each figure.

I know how to change the title of a figure:

fig = pylab.gcf()
fig.canvas.set_window_title('Test')

But how do I change the default window title (So that it will be "Test 1", "Test 2" etc.), so that I will not need to change the window title each time?

I did not find a key in the mpl.rcParams

1
  • For those of you who came here wanting to change the window title after the figure creation, the above no longer works. You now need to do something like plt.get_current_fig_manager().set_window_title("My new window title")
    – shcrela
    Commented Feb 6 at 13:20

2 Answers 2

13

Edit: my answer does not change the defaults, as requested by OP, but provides a way to define figure title at figure creation.

When creating a figure using matplotlib.pyplot.subplots, there is an optional argument num that, even if not documented as such (as far as I could search), is later used as figure title:

import matplotlib.pyplot as plt
fig, axes = plt.subplots(nrows=2, num="some nice window title")
plt.ion()  # to make plot non-blocking, i.e. if multiple plots are launched
fig.show()

It is also used as default filename when saving the plot, which is a very neat feature.

(Caution: even if not documented, this num value is also a key to this figure. So, take care not to reuse the same value.)

And here's the result: enter image description here

5
  • 1
    It's nice, thanks, but it is not exactly what I needed. I wanted to change the default behavior.
    – Oren
    Commented Nov 20, 2019 at 9:31
  • 4
    There is a comment about the 'num' kwargs in the matplotlib.pyplot.figure documentation 3.2.1. If not provided, a new figure will be created, and the figure number will be incremented. The figure objects holds this number in a number attribute. If num is provided, and a figure with this id already exists, make it active, and returns a reference to it. If this figure does not exists, create it and returns it. If num is a string, the window title will be set to this figure's num.
    – bvanlew
    Commented Mar 31, 2020 at 6:55
  • 3
    Even if this doesn't answer the OPs question, it seems to be useful because an internet search to the question how to set a specific window title finds this :-)
    – AstroFloyd
    Commented Jul 19, 2022 at 12:11
  • @AstroFloyd, and that is how I found it :) Commented Mar 26 at 22:38
  • @Oren You can always define a wrapper around e.g. pyplot.subplot() to enforce a default behavior that suits you better: def my_subplot(plt: matplotlib.pyplot, *args, **kwargs): if not 'num' in kwargs: kwargs['num'] = 'I want it *that* way'; return plt.subplot(*args, **kwargs) Maybe you want to define that as a method of a subclass of pyplot that you use instead. Commented Mar 26 at 22:43
3

There is no key in mpl.rcParams since the default title is hardcoded in the backends. For example, have a look at the figure initialization code of the QT5 backend:

self.window.setWindowTitle("Figure %d" % num)

This means you cannot change the default window title unless you change the code of the matplotlib module itself.

1

Your Answer

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

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