Python matplotlib.pyplot.close() Function Example
Below are the examples by which we can understand about how to close Matplotlib figure in Python using Matplotlib.pyplot.close() function:
Closing PyPlot Windows Using matplotlib.pyplot.close() Function
In this example, the code uses Matplotlib to create a 2D plot with two overlaid images. The first image (`Z1`) represents a checkerboard pattern, displayed in binary colors. The second image (`Z2`) is generated using a custom function (`geeks`) and displayed in green with partial transparency. Then using close() function, windows are closed.
Python3
# Implementation of matplotlib function import matplotlib.pyplot as plt import numpy as np from matplotlib.colors import LogNorm dx, dy = 0.015 , 0.05 x = np.arange( - 4.0 , 4.0 , dx) y = np.arange( - 4.0 , 4.0 , dy) X, Y = np.meshgrid(x, y) extent = np. min (x), np. max (x), np. min (y), np. max (y) Z1 = np.add.outer( range ( 8 ), range ( 8 )) % 2 plt.imshow(Z1, cmap = "binary_r" , interpolation = 'nearest' , extent = extent, alpha = 1 ) def geeks(x, y): return ( 1 - x / 2 + x * * 5 + y * * 6 ) * np.exp( - (x * * 2 + y * * 2 )) Z2 = geeks(X, Y) x = plt.imshow(Z2, cmap = "Greens" , alpha = 0.7 , interpolation = 'bilinear' , extent = extent) plt.close() plt.title( 'matplotlib.pyplot.close Example' ) plt.show() |
Output:
Close Matplotlib Figure Using Triangular Mesh and close() Function
In this example, the code utilizes Matplotlib to generate a triangular mesh plot with two overlaid images. The first image (Z1
) displays a checkerboard pattern, and the second image (Z2
) is generated using a custom function. The code also includes a call to plt.close(1)
to close the default figure, and it proceeds to create a triangular mesh plot with a color bar using the matplotlib.tri
module.
Python3
# Implementation of matplotlib function import matplotlib.pyplot as plt import numpy as np from matplotlib.colors import LogNorm import matplotlib.tri as tri dx, dy = 0.015 , 0.05 x = np.arange( - 4.0 , 4.0 , dx) y = np.arange( - 4.0 , 4.0 , dy) X, Y = np.meshgrid(x, y) extent = np. min (x), np. max (x), np. min (y), np. max (y) Z1 = np.add.outer( range ( 8 ), range ( 8 )) % 2 plt.imshow(Z1, cmap = "binary_r" , interpolation = 'nearest' , extent = extent, alpha = 1 ) def geeks(x, y): return ( 1 - x / 2 + x * * 5 + y * * 6 ) * np.exp( - (x * * 2 + y * * 2 )) Z2 = geeks(X, Y) x = plt.imshow(Z2, cmap = "Greens" , alpha = 0.7 , interpolation = 'bilinear' , extent = extent) plt.close( 1 ) ang = 40 rad = 10 radm = 0.35 radii = np.linspace(radm, 0.95 , rad) angles = np.linspace( 0 , 0.5 * np.pi, ang) angles = np.repeat(angles[..., np.newaxis], rad, axis = 1 ) angles[:, 1 :: 2 ] + = np.pi / ang x = (radii * np.cos(angles)).flatten() y = (radii * np.sin(angles)).flatten() z = (np.sin( 4 * radii) * np.cos( 4 * angles)).flatten() triang = tri.Triangulation(x, y) triang.set_mask(np.hypot(x[triang.triangles].mean(axis = 1 ), y[triang.triangles].mean(axis = 1 )) < radm) tpc = plt.tripcolor(triang, z, shading = 'flat' ) plt.colorbar(tpc) plt.plasma() plt.title( 'matplotlib.pyplot.close() Example' ) plt.show() |
Output:
Matplotlib.pyplot.close() in Python
Matplotlib close() function in pyplot module of the matplotlib library is used to close a figure window. This function is designed to close a figure window or a set of figure windows. When called without any arguments, it closes the currently active figure. Alternatively, we can pass a figure number or a reference to a figure object as an argument to close a specific figure.
Contact Us