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