📜  Python中的 Matplotlib.pyplot.get_figlabels()

📅  最后修改于: 2022-05-13 01:55:43.491000             🧑  作者: Mango

Python中的 Matplotlib.pyplot.get_figlabels()

Matplotlib是Python中的一个库,它是 NumPy 库的数值数学扩展。 Pyplot是 Matplotlib 模块的基于状态的接口,它提供了一个类似 MATLAB 的接口。在 Pyplot 中可以使用各种图,包括线图、等高线图、直方图、散点图、3D 图等。

matplotlib.pyplot.get_figlabels() 方法

matplotlib 库的 pyplot 模块中的get_figlabels() 方法用于获取现有图形标签的列表。

下面的示例说明了 matplotlib.pyplot 中的 matplotlib.pyplot.get_figlabels()函数:

示例 1:

import matplotlib.pyplot as plt
import numpy as np
     
  
t = np.arange(0.01, 5.0, 0.01)
s = np.exp(-t)
     
plt.plot(t, s)
plt.ylim(1, 0)
plt.ylabel('Display Y-axis Label', fontweight ='bold')
plt.grid(True)
  
w = plt.get_figlabels()
  
plt.text(1.5, 0.58,
         "List of existing figure labels : \n"
         + str(w),
         fontsize = 12)
     
plt.title('matplotlib.pyplot.get_figlabels() function\
 Example', fontweight ="bold") 
  
plt.show()

输出:

示例 2:

import matplotlib.pyplot as plt
import matplotlib as mpl
import matplotlib.patches as patches
import numpy as np
   
      
mpl.rcParams['figure.dpi'] = 80
mpl.rcParams['savefig.dpi'] = 80 
  
def redraw(event):
      
    if  np.size(plt.get_figlabels()):
        ax.clear()
        drawRectangles(ax)
        fig.canvas.draw()
    else:
        pass
   
   
def drawRectangles(ax):
      
    td2dis = ax.transData
    coords = td2dis.transform([0.2, 0.5])
    tr = mpl.transforms.Affine2D().rotate_deg_around(coords[0],
                                                     coords[1], 10)
    t = td2dis + tr
    rec0 = patches.Rectangle((0.5, 0.5),
                             0.25, 0.2, 
                             color = 'green', 
                             alpha = 0.4)
    ax.add_patch(rec0)
    rect1 = patches.Rectangle((0.5, 0.5),
                              0.25, 0.2,
                              color = 'green',
                              alpha = 0.7,
                              transform = t)
    ax.add_patch(rect1);
    plt.title('matplotlib.pyplot.get_figlabels() function Example', fontweight ="bold")
    plt.grid()
   
   
figSize = (8, 6)
fig = plt.figure("Patch rotate", figsize = figSize)
   
ax = fig.add_subplot(111)
ax.set_xlim(0, 1);ax.set_ylim(0, 1);
  
fig.canvas.mpl_connect('resize_event', redraw)
drawRectangles(ax) 
  
plt.show()

输出: