Python中的 Matplotlib.pyplot.setp()函数
Matplotlib是Python中的一个库,它是 NumPy 库的数值数学扩展。 Pyplot是Matplotlib模块的基于状态的接口,它提供了一个类似 MATLAB 的接口。在 Pyplot 中可以使用各种图,包括线图、等高线图、直方图、散点图、3D 图等。
Matplotlib.pyplot.setp()函数
matplotlib 库的 pyplot 模块中的 setp ()函数用于设置艺术家对象的属性。
Syntax: matplotlib.pyplot.setp(obj, \*args, \*\*kwargs)
Parameters: This method accept the following parameters that are described below:
- obj: This parameter is the artist object.
- **kwargs: There are different keyword arguments which are accepted.
Returns: This method does not returns any value.
下面的示例说明了 matplotlib.pyplot 中的 matplotlib.pyplot.setp()函数:
示例 1:
Python3
#Implementation of matplotlib function
import numpy as np
import matplotlib.pyplot as plt
def tellme(s):
plt.title(s, fontsize=16)
plt.draw()
plt.clf()
plt.setp(plt.gca(), autoscale_on=False)
tellme('matplotlib.pyplot.setp() function Example')
plt.show()
Python3
# Implementation of matplotlib function
import matplotlib
import numpy as np
import matplotlib.cm as cm
import matplotlib.pyplot as plt
delta = 0.25
x = np.arange(-3.0, 5.0, delta)
y = np.arange(-1.3, 2.5, delta)
X, Y = np.meshgrid(x, y)
Z = (np.exp(-X**2 - Y**2) - np.exp(-(X - 1)**2 - (Y - 1)**2))
im = plt.imshow(Z, interpolation='bilinear',
origin='lower',
cmap="bone",
extent=(-3, 3, -2, 2))
levels = np.arange(-1.2, 1.6, 0.2)
CS = plt.contour(Z, levels,
origin='lower',
cmap='Greens',
linewidths=2,
extent=(-3, 3, -2, 2))
zc = CS.collections[6]
plt.setp(zc, linewidth=2)
plt.clabel(CS, levels,
inline=1,
fmt='%1.1f',
fontsize=14)
plt.title('matplotlib.pyplot.setp() Example')
plt.show()
输出:
示例 2:
Python3
# Implementation of matplotlib function
import matplotlib
import numpy as np
import matplotlib.cm as cm
import matplotlib.pyplot as plt
delta = 0.25
x = np.arange(-3.0, 5.0, delta)
y = np.arange(-1.3, 2.5, delta)
X, Y = np.meshgrid(x, y)
Z = (np.exp(-X**2 - Y**2) - np.exp(-(X - 1)**2 - (Y - 1)**2))
im = plt.imshow(Z, interpolation='bilinear',
origin='lower',
cmap="bone",
extent=(-3, 3, -2, 2))
levels = np.arange(-1.2, 1.6, 0.2)
CS = plt.contour(Z, levels,
origin='lower',
cmap='Greens',
linewidths=2,
extent=(-3, 3, -2, 2))
zc = CS.collections[6]
plt.setp(zc, linewidth=2)
plt.clabel(CS, levels,
inline=1,
fmt='%1.1f',
fontsize=14)
plt.title('matplotlib.pyplot.setp() Example')
plt.show()
输出: