Python中的 Matplotlib.figure.Figure.set_canvas()
Matplotlib是Python中的一个库,它是 NumPy 库的数值数学扩展。 figure 模块提供了顶级 Artist,即 Figure,其中包含所有绘图元素。该模块用于控制所有绘图元素的子图和顶级容器的默认间距。
matplotlib.figure.Figure.set_canvas() 方法
matplotlib 库的set_canvas() 方法图形模块用于设置包含图形的画布。
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.
下面的示例说明了 matplotlib.figure 中的 matplotlib.figure.Figure.set_canvas()函数:
示例 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()
输出:
示例 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()
输出: