I am using matplotlib with the %matplotlib widget
in a Jupyter lab notebook. When I display a figure, there is "Figure 1" displayed just above it. How can I get rid of it?
I have found that plt.figure(num='', frameon=False)
does show a figure without this prefix, however it is not interactive and I cannot update its content later on (which is the reason why I am using the above magic in the first place).
You can add this in next cell after-the-fact to change the window title of the prior interactive plot to empty:
#based on https://stackoverflow.com/a/73802955/8508004
man = plt.get_current_fig_manager()
man.set_window_title("") #alt: set it to space(s) `man.set_window_title(" ")`
To run all in one cell, to make plot and have title not displayed in one step, it would be like this where you assign num
keyword to a string filled with just space(s), adapting plot code from the Basic Example in ipympl documentation:
%matplotlib ipympl
import matplotlib.pyplot as plt
import numpy as np
fig, ax = plt.subplots(num=' ') #based on https://stackoverflow.com/a/45632377/8508004 , although later I understood varagawal's comment at https://stackoverflow.com/questions/5812960/change-figure-window-title-in-pylab/75969589#comment111311103_46054686 was trying to say it too
x = np.linspace(0, 2*np.pi, 100)
y = np.sin(3*x)
ax.plot(x, y)
plt.show()
Explanation:
From here, "plt.subplots()
passes additional keyword arguments to plt.figure
. Therefore, the num
keyword will do what you want." This is also how you'd specify the exact number after Figure
if you preferred, by instead passing an integer.
Passing '' without the space(s) into num
, as in the last paragraph of the post, will not work because there's nothing to be evaluated there as a string or integer, and so the automatic numbering mechanism is invoked.
In the case if assigning num
to a space (or spaces), the quoted space is detected as a string and that string is set as the window title. As pointed by varagrawal here. Current quote about this in the matplotlib documentation:
"If num is a string, the figure label and the window title is set to this value."
When not working with subplots, this seems to work in a single Jupyter cell:
%matplotlib ipympl
#based on https://stackoverflow.com/a/46054686/8508004
import matplotlib.pyplot as plt
x = [1,2,3,4,5]
y = [2,4,6,8,10]
fig = plt.figure(num=" ") # simply, `fig = plt.figure(" ")` works as well; use of `num` preferred as more explicit
plt.plot(x,y)
plt.show()
The explanation in the section above concerning num
covers how this works. I separated it out though because you do have the option in this simpler situation of just adding fig = plt.figure(" ")
and not explicitly assigning num
. Or include a string for a title you prefr in place of the quoted space.
(That code example was adapted from here, although I found the specific approach offered there currently only works in freshly started kernel, if you execute it twice.)
If you left out the fig = plt.figure(...
line, you can also use the 'after-the-fact' approach I note at the start of the section above to run code, i.e., plt.get_current_fig_manager().set_window_title("")
, in the next cell to clear the window title.
Detailed sources: