當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Python Matplotlib.pyplot.findobj()用法及代碼示例

Matplotlib是Python中令人驚歎的可視化庫,用於二維陣列圖。 Matplotlib是一個基於NumPy數組的多平台數據可視化庫,旨在與更廣泛的SciPy堆棧配合使用。

matplotlib.pyplot.findobj()

此函數用於遞歸查找藝術家中包含的藝術家的所有實例。創建過濾器以匹配藝術家對象,該藝術家對象查找並返回匹配藝術家的列表。藝術家對象是指matplotlib.artist負責在畫布上繪製顏料的類。

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


參數:

  1. match:此參數用於創建過濾器以匹配搜索的藝術家對象。這可能是三件事之一;
    • None:這將返回藝術家中的所有對象。
    • A function:具有簽名的函數,例如def match(artist:Artist)->布爾值。此函數的結果包含藝術家,該藝術家為其返回True。
    • 一個類實例:此結果包含相同類或其子類之一的藝術家(實例檢查),例如Line2D
  2. include_self:這個參數接受一個布爾值,並且對我來說包含它本身以檢查匹配項列表。

返回值:它返回藝術家列表

範例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()

輸出:

python-matplotlib-findobj-1

範例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()

輸出:
matplotlib.pyplot.findobj()




相關用法


注:本文由純淨天空篩選整理自RajuKumar19大神的英文原創作品 Matplotlib.pyplot.findobj() in Python。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。