当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Python Matplotlib.figure.Figure.gca()用法及代码示例


Matplotlib是Python中的一个库,它是数字的-NumPy库的数学扩展。 Figure模块提供了顶层Artist,即Figure,其中包含所有绘图元素。此模块用于控制所有图元的子图和顶层容器的默认间距。

matplotlib.figure.Figure.gca()方法

matplotlib库的gca()方法图形模块用于获取当前轴。

用法: gca(self, **kwargs)


参数:此方法不接受任何参数。

返回:该方法返回当前轴。

以下示例说明了matplotlib.figure中的matplotlib.figure.Figure.gca()函数:

范例1:

# Implementation of matplotlib function 
import matplotlib.pyplot as plt 
import numpy as np 
import matplotlib.gridspec as gridspec 
from mpl_toolkits.axes_grid1 import make_axes_locatable 
   
  
plt.close('all') 
arr = np.arange(100).reshape((10, 10)) 
fig = plt.figure(figsize =(4, 4)) 
  
im = plt.imshow(arr, 
                interpolation ="none", 
                cmap ="plasma") 
   
divider = make_axes_locatable(fig.gca()) 
cax = divider.append_axes("left", 
                          "15 %",  
                           pad ="30 %") 
  
plt.colorbar(im, cax = cax) 
   
fig.suptitle('matplotlib.figure.Figure.gca()\ 
 function Example', fontweight ="bold")  
  
plt.show()

输出:

范例2:

#Implementation of matplotlib function 
import matplotlib.pyplot as plt 
from matplotlib.tri import Triangulation 
from matplotlib.patches import Polygon 
import numpy as np 
   
      
ang = 3
radi = 8
radii = np.linspace(0.25, 0.95, radi) 
  
res = np.linspace(0, 4 * np.pi, ang) 
res = np.repeat(res[..., np.newaxis], radi, axis=1) 
res[:, 1::2] += np.pi / ang 
  
x = (radii*np.cos(2*res)).flatten() 
y = (radii*np.sin(2*res)).flatten() 
  
triang = Triangulation(x, y) 
triang.set_mask(np.hypot(x[triang.triangles].mean(axis=1), 
                         y[triang.triangles].mean(axis=1)) 
                < 0.25) 
     
trifinder = triang.get_trifinder() 
     
fig = plt.figure() 
plt.triplot(triang, 'go-') 
polygon = Polygon([[0, 0], [0, 0]], facecolor='r') 
     
def update_polygon(tri):
  
    if tri == -1:
        points = [0, 0, 0] 
      
    else:
        points = triang.triangles[tri] 
      
    xs = triang.x[points] 
    ys = triang.y[points] 
    polygon.set_xy(np.column_stack([xs, ys])) 
     
     
def motion_notify(event):
      
    if event.inaxes is None:
        tri = -1
      
    else:
        tri = trifinder(event.xdata, event.ydata) 
      
    update_polygon(tri) 
      
    fig.suptitle('matplotlib.figure.Figure.gca()\ 
    function Example\n\n Potion number:%i' % tri, 
                 fontweight="bold") 
      
    event.canvas.draw() 
     
update_polygon(-1) 
fig.gca().add_patch(polygon) 
plt.gcf().canvas.mpl_connect('motion_notify_event', 
                             motion_notify) 
     
plt.show()

输出:





注:本文由纯净天空筛选整理自SHUBHAMSINGH10大神的英文原创作品 Matplotlib.figure.Figure.gca() in Python。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。