📌  相关文章
📜  Python中的 Matplotlib.axis.Axis.set_transform()函数

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

Python中的 Matplotlib.axis.Axis.set_transform()函数

Matplotlib是Python中的一个库,它是 NumPy 库的数值数学扩展。它是Python中用于二维数组图的惊人可视化库,用于处理更广泛的 SciPy 堆栈。

Matplotlib.axis.Axis.set_transform()函数

matplotlib 库的轴模块中的Axis.set_transform()函数用于设置艺术家变换。

下面的示例说明了 matplotlib.axis 中的 matplotlib.axis.Axis.set_transform()函数:

示例 1:

Python3
# Implementation of matplotlib function
from matplotlib.axis import Axis
import numpy as np  
import matplotlib.pyplot as plt  
import matplotlib.transforms as mtransforms  
  
delta = 0.5
    
x = y = np.arange(-2.0, 4.0, delta)  
X, Y = np.meshgrid(x**2, y)  
    
Z1 = np.exp(-X**2 - Y**2)  
Z2 = np.exp(-(X - 1)**2 - (Y - 1)**2)  
Z = (Z1 - Z2)  
      
transform = mtransforms.Affine2D().rotate_deg(30)  
fig, ax = plt.subplots()  
          
im = ax.imshow(Z, interpolation ='none',  
               origin ='lower',  
               extent =[-2, 4, -3, 2],   
               clip_on = True)  
      
trans_data = transform + ax.transData  
Axis.set_transform(im, trans_data)  
      
x1, x2, y1, y2 = im.get_extent()  
ax.plot([x1, x2, x2, x1, x1],   
        [y1, y1, y2, y2, y1],  
        "ro-",  
        transform = trans_data)  
      
ax.set_xlim(-5, 5)  
ax.set_ylim(-4, 4)   
  
fig.suptitle('matplotlib.axis.Axis.set_transform() \
function Example\n', fontweight ="bold")  
    
plt.show()


Python3
# Implementation of matplotlib function
from matplotlib.axis import Axis
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)  
Axis.set_transform(col, trans)   
      
ax1.add_collection(col, autolim = True)  
col.set_color(colors) 
  
fig.suptitle('matplotlib.axis.Axis.set_transform() \
function Example\n', fontweight ="bold")  
    
plt.show()


输出:

示例 2:

Python3

# Implementation of matplotlib function
from matplotlib.axis import Axis
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)  
Axis.set_transform(col, trans)   
      
ax1.add_collection(col, autolim = True)  
col.set_color(colors) 
  
fig.suptitle('matplotlib.axis.Axis.set_transform() \
function Example\n', fontweight ="bold")  
    
plt.show() 

输出: