Open In App

Matplotlib.figure.Figure.savefig() in Python

Matplotlib is a library in Python and it is numerical – mathematical extension for NumPy library. The figure module provides the top-level Artist, the Figure, which contains all the plot elements. This module is used to control the default spacing of the subplots and top level container for all plot elements.

matplotlib.figure.Figure.savefig() method

The savefig() method figure module of matplotlib library is used to save the current figure.



Syntax: savefig(self, fname, *, transparent=None, **kwargs)

Parameters: This method accept the following parameters that are discussed below:



  • fname :This parameter is the file name string.

Returns: This method does not returns any value.

Below examples illustrate the matplotlib.figure.Figure.savefig() function in matplotlib.figure:

Example 1:




# Implementation of matplotlib function  
import numpy as np
import matplotlib.cm as cm
import matplotlib.mlab as mlab
import matplotlib.pyplot as plt
   
fig, ax = plt.subplots()
s = ax.scatter([1, 2, 3], [4, 5, 6])
s.set_url('http://www.google.com')
   
fig.savefig('scatter.svg')
  
fig.suptitle("""matplotlib.figure.Figure.savefig()
function Example\n\n""", fontweight ="bold") 
    
plt.show() 

Output:

Example 2:




# Implementation of matplotlib function  
import numpy as np
import matplotlib.cm as cm
import matplotlib.mlab as mlab
import matplotlib.pyplot as plt
   
fig, ax = plt.subplots()
delta = 0.025
x = y = np.arange(-3.0, 3.0, delta)
X, Y = np.meshgrid(x, y)
Z1 = np.exp(-X**2 - Y**2)
Z2 = np.exp(-(X - 1)**2 - (Y - 1)**2)
Z = (Z1 - Z2) * 2
   
im = ax.imshow(Z,
               interpolation ='bilinear',
               cmap = cm.gray,
               origin ='lower',
               extent =[-3, 3, -3, 3])
   
fig.savefig('image.svg')
  
fig.suptitle("""matplotlib.figure.Figure.savefig()
function Example\n\n""", fontweight ="bold") 
    
plt.show() 

Output:


Article Tags :