In Python, when you use matplotlib.pyplot.show(), it displays all the figures you've created but does not automatically create a new figure for you. If you want to ensure that a new figure is created every time you call plt.show(), you need to explicitly call plt.figure() before plotting.
plt.figure() and plt.show()If you want to create a new figure and then display it, you can do:
pythonimport matplotlib.pyplot as plt # Create a new figure plt.figure() # Plot something (optional) plt.plot([1, 2, 3], [4, 5, 6]) # Show the figure plt.show()
If you want to ensure that a new figure is created every time you call plt.show(), you can create a custom function:
pythonimport matplotlib.pyplot as plt def show_with_new_figure(): plt.figure() # Create a new figure plt.show() # Show the figure # Usage show_with_new_figure()
plt.figure() creates a new figure window.plt.show() displays the current figure(s).plt.show() multiple times in a script, it will block execution until all figure windows are closed. To avoid this, use plt.ion() for interactive mode or close figures manually with plt.close().Would you like help with a specific use case or more advanced customization?