📌  相关文章
📜  Python中的 Matplotlib.artist.Artist.get_transform()

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

Python中的 Matplotlib.artist.Artist.get_transform()

Matplotlib是Python中的一个库,它是 NumPy 库的数值数学扩展。 Artist 类包含呈现为 FigureCanvas 的对象的 Abstract 基类图中所有可见元素都是 Artist 的子类。

Matplotlib.artist.Artist.get_transform() 方法

matplotlib库的artist模块中的get_transform()方法用于获取该艺术家使用的Transform实例

以下示例说明了 matplotlib 中的 matplotlib.artist.Artist.get_transform()函数:

示例 1:

# Implementation of matplotlib function
from matplotlib.artist import Artist 
import numpy as np  
import matplotlib.pyplot as plt 
import matplotlib.transforms as mtransforms 
    
    
fig, ax = plt.subplots()  
  
l1, = ax.plot([0.1, 0.5, 0.9], 
              [0.1, 0.9, 0.5],
              "bo-") 
  
l2, = ax.plot([0.1, 0.5, 0.9],
              [0.5, 0.2, 0.7],
              "ro-") 
     
for l in [l1, l2]: 
    xx = l.get_xdata() 
    yy = l.get_ydata() 
    shadow, = ax.plot(xx, yy) 
    shadow.update_from(l) 
         
    ot = mtransforms.offset_copy(Artist.get_transform(l), 
                                 ax.figure, 
                                 x = 4.0, y =-6.0, 
                                 units ='points') 
      
    shadow.set_transform(ot) 
  
plt.title("""matplotlib.artist.Artist.get_transform()
function Example""", fontweight="bold")
  
plt.show()

输出:

示例 2:

# Implementation of matplotlib function
from matplotlib.artist import Artist 
import matplotlib.pyplot as plt 
from matplotlib import collections, colors, transforms 
import numpy as np 
  
     
nverts = 50
npts = 100
     
r = np.arange(nverts) 
theta = np.linspace(0, 2 * np.pi, nverts) 
  
xx = r * np.sin(theta) 
yy = r * np.cos(theta) 
spiral = np.column_stack([xx, yy]) 
     
rs = np.random.RandomState(19680801) 
     
xyo = rs.randn(npts, 2) 
     
colors = [colors.to_rgba(c) 
          for c in plt.rcParams['axes.prop_cycle'].by_key()['color']] 
     
fig, ax1 = plt.subplots() 
     
col = collections.RegularPolyCollection( 
    7, sizes = np.abs(xx) * 10.0,  
    offsets = xyo,  
    transOffset = ax1.transData) 
    
trans = transforms.Affine2D().scale(fig.dpi / 72.0) 
Artist.set_transform(col, trans)  
    
ax1.add_collection(col, autolim = True) 
col.set_color(colors)
  
plt.title("""matplotlib.artist.Artist.set_transform()
function Example""", fontweight="bold")
  
plt.show()

输出:

Value Return by get_transform() :
 Affine2D(
    [[1.38888889 0.         0.        ]
     [0.         1.38888889 0.        ]
     [0.         0.         1.        ]])