Monday, November 18, 2024
Google search engine
HomeLanguagesMatplotlib.pyplot.findobj() in Python

Matplotlib.pyplot.findobj() in Python

Matplotlib is an amazing visualization library in Python for 2D plots of arrays. Matplotlib is a multi-platform data visualization library built on NumPy arrays and designed to work with the broader SciPy stack.

matplotlib.pyplot.findobj()

This function is used to recursively find all instances of artists contained in the artist. Filters are created to match for the artist object which finds and returns a list of matched artists. An artist object refers to the object of matplotlib.artist class that is responsible for rendering the paint on the canvas.

Syntax: matplotlib.pyplot.findobj(o=None, match=None, include_self=True)

Parameters:

  1. 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
  2. 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

Example 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()


Output:

python-matplotlib-findobj-1

Example 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()


Output:
matplotlib.pyplot.findobj()

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments