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


Python Matplotlib.axes.Axes.set_figure()用法及代碼示例

Matplotlib是Python中的一個庫,它是數字的-NumPy庫的數學擴展。軸類包含大多數圖形元素:Axis,Tick,Line2D,Text,Polygon等,並設置坐標係。 Axes實例通過callbacks屬性支持回調。

matplotlib.axes.Axes.set_figure()函數

matplotlib庫的axiss模塊中的Axes.set_figure()函數用於為此軸設置圖形。

用法: Axes.set_figure(self, fig)


參數:此方法僅接受一個參數。

  • fig:此參數是Figure實例。

返回值:此方法不返回任何值。

以下示例說明了matplotlib.axes中的matplotlib.axes.Axes.set_figure()函數:

範例1:

# Implementation of matplotlib function 
import numpy as np 
import matplotlib.pyplot as plt 
import matplotlib.lines as lines 
import matplotlib.transforms as mtransforms 
import matplotlib.text as mtext 
   
   
class GFGfun(lines.Line2D):
    def __init__(self, *args, **kwargs):
        self.text = mtext.Text(0, 0, '') 
        lines.Line2D.__init__(self, *args, **kwargs) 
        self.text.set_text(self.get_label()) 
   
    def set_figure(self, figure):
        self.text.set_figure(figure) 
        lines.Line2D.set_figure(self, figure) 
   
    def set_axes(self, axes):
        self.text.set_axes(axes) 
        lines.Line2D.set_axes(self, axes) 
   
    def set_transform(self, transform):
        # 2 pixel offset 
        texttrans = transform + mtransforms.Affine2D().translate(2, 2) 
        self.text.set_transform(texttrans) 
        lines.Line2D.set_transform(self, transform) 
   
    def set_data(self, x, y):
        if len(x):
            self.text.set_position((x[-1], y[-1])) 
   
        lines.Line2D.set_data(self, x, y) 
   
    def draw(self, renderer):
        lines.Line2D.draw(self, renderer) 
        self.text.draw(renderer) 
   
   
np.random.seed(10**7) 
   
   
fig, ax = plt.subplots() 
x, y = np.random.rand(2, 20) 
line = GFGfun(x, y, mfc ='green', 
              ms = 12,  
              label ='Label') 
   
line.text.set_color('green') 
line.text.set_fontsize(16) 
   
ax.add_line(line) 
   
fig.suptitle('matplotlib.axes.Axes.set_figure() \ 
function Example\n\n', fontweight ="bold") 
  
plt.show()

輸出:

範例2:

# Implementation of matplotlib function 
import numpy as np 
import matplotlib.pyplot as plt 
import matplotlib.lines as lines 
import matplotlib.transforms as mtransforms 
import matplotlib.text as mtext 
   
   
class GFGfun(lines.Line2D):
    def __init__(self, *args, **kwargs):
        self.text = mtext.Text(0, 0, '') 
        lines.Line2D.__init__(self, *args, **kwargs) 
        self.text.set_text(self.get_label()) 
   
    def set_figure(self, figure):
        self.text.set_figure(figure) 
        lines.Line2D.set_figure(self, figure) 
   
   
np.random.seed(10**7) 
   
   
fig, ax = plt.subplots() 
x, y = np.random.rand(2, 10) 
line = GFGfun(x, y, mfc ='green', 
              ms = 12, 
              label ='Label') 
   
line.text.set_color('green') 
line.text.set_fontsize(16) 
   
ax.add_line(line) 
   
fig.suptitle('matplotlib.axes.Axes.set_figure() \ 
function Example\n\n', fontweight ="bold") 
  
plt.show()

輸出:




相關用法


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