Python中的 Matplotlib.pyplot.findobj()
Matplotlib是Python中用于数组二维图的惊人可视化库。 Matplotlib 是一个基于 NumPy 数组构建的多平台数据可视化库,旨在与更广泛的 SciPy 堆栈配合使用。
matplotlib.pyplot.findobj()
此函数用于递归查找艺术家中包含的所有艺术家实例。创建过滤器以匹配艺术家对象,该对象查找并返回匹配艺术家的列表。艺术家对象是指matplotlib.artist
类的对象,它负责在画布上渲染绘画。
Syntax: matplotlib.pyplot.findobj(o=None, match=None, include_self=True)
Parameters:
- match: This parameter is used for the creating filter to match for the searched artist object. This can be one of three things;
- None: This returns all objects in artist.
- A function: A function with signature such as def match(artist: Artist) -> boolean. The result from this function has artists for which the function returns True.
- A class instance: The result of this contain artist of the same class or one of its subclasses(isinstance check), eg, Line2D
- include_self:This parameter accepts a boolean value and it includes itself to me checked for the list of matches.
Returns: It returns a list of Artist
示例 1:
import matplotlib.pyplot as plt
import numpy as np
h = plt.figure()
plt.plot(range(1,11),
range(1,11),
gid = 'dummy_data')
legend = plt.legend(['the plotted line'])
plt.title('figure')
axis = plt.gca()
axis.set_xlim(0,5)
for p in set(h.findobj(lambda x: x.get_gid() == 'dummy_data')):
p.set_ydata(np.ones(10)*10.0)
plt.show()
输出:
示例 2:
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.text as text
m = np.arange(3, -4, -.2)
n = np.arange(3, -4, -.2)
o = np.exp(m)
p = o[::-1]
figure, axes = plt.subplots()
plt.plot(m, o, 'k--', m, p,
'k:', m, o + p, 'k')
plt.legend((' Modelset', 'Dataset',
'Total string length'),
loc ='upper center',
shadow = True)
plt.ylim([-1, 10])
plt.grid(True)
plt.xlabel(' Modelset --->')
plt.ylabel(' String length --->')
plt.title('Min. Length of String')
# Helper function
def find_match(x):
return hasattr(x, 'set_color') and not hasattr(x, 'set_facecolor')
# calling the findobj function
for obj in figure.findobj(find_match):
obj.set_color('black')
# match on class instances
for obj in figure.findobj(text.Text):
obj.set_fontstyle('italic')
plt.show()
输出: