Set matplotlib default figure window title

℡╲_俬逩灬. 提交于 2021-02-08 23:37:57

问题


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

Thanks


回答1:


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:




回答2:


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 (https://github.com/matplotlib/matplotlib/blob/c1a3c030c66f512c6f79e4f45b0870b68921320c/lib/matplotlib/backends/backend_qt5.py#L554):

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.



来源:https://stackoverflow.com/questions/38307438/set-matplotlib-default-figure-window-title

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!