Search code examples
pythonmatplotlibjupyter-notebookjupyter-labmatplotlib-widget

How can I plot an interactive matplotlib figure without the figure number in a Jupyter notebook?


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).


Solution

  • Involving subplots:

    You can add this in next cell after-the-fact to change the window title of the interactive plot just above to empty:

    #based on https://stackoverflow.com/a/73802955/8508004
    man = plt.get_current_fig_manager()
    man.set_window_title(" ")
    

    To run all in one cell to make plot initially it would be like this, 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()
    

    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.



    If interactive without subplot:

    When not working with subplots, this seems to work in a single Jupyter cell:

    %matplotlib ipympl
    #based on https://stackoverflow.com/a/75969589/8508004
    import matplotlib.pyplot as plt
    x = [1,2,3,4,5]
    y = [2,4,6,8,10]
    plt.plot(x,y)
    plt.get_current_fig_manager().set_window_title('I control this')
    plt.show()
    

    Change that to an emptry string, like plt.get_current_fig_manager().set_window_title(' '), to clear it.


    Detailed sources:

    1. https://stackoverflow.com/a/73802955/8508004
    2. https://stackoverflow.com/a/45632377/8508004
    3. Comment by varagrawal
    4. https://stackoverflow.com/a/75969589/8508004