Open In App

Matplotlib.figure.Figure.set_canvas() in Python

Improve
Improve
Like Article
Like
Save
Share
Report

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.set_canvas() method

The set_canvas() method figure module of matplotlib library is used to set the canvas that contains the figure.

Syntax: set_canvas(self, canvas)

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

  • canvas : This parameter is the FigureCanvas.

Returns: This method returns the axes.

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

Example 1:




# Implementation of matplotlib function
import matplotlib.pyplot as plt
  
  
def new_figure(): 
  
    fig = plt.figure()
    plt.plot([0, 1], [2, 3])
    plt.close(fig)
    return fig
  
def show_figure(fig):
  
    dummy = plt.figure()
    new_manager = dummy.canvas.manager
    new_manager.canvas.figure = fig
    fig.set_canvas(new_manager.canvas)
  
fig = new_figure()
show_figure(fig)
  
fig.suptitle("""matplotlib.figure.Figure.set_canvas()
function Example\n\n""", fontweight ="bold")    
  
plt.show()


Output:

Example 2:




# Implementation of matplotlib function
import numpy as np
import matplotlib.pyplot as plt
  
   
x = np.arange(16)
y = np.sin(x / 3)
   
fig, ax = plt.subplots()
   
ax.step(x, y + 2, color ='green')
ax.plot(x, y + 2, 'o--', color ='black', alpha = 0.3)
  
def show_figure(fig):
  
    dummy = plt.figure()
    new_manager = dummy.canvas.manager
    new_manager.canvas.figure = fig
    fig.set_canvas(new_manager.canvas)
  
show_figure(fig)
  
fig.suptitle("""matplotlib.figure.Figure.set_canvas()
function Example\n\n""", fontweight ="bold")    
  
plt.show()


Output:



Last Updated : 03 May, 2020
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads