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


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

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

matplotlib.axes.Axes.get_title()函數

matplotlib庫的axiss模塊中的Axes.get_title()函數用於獲取軸標題。

用法: Axes.get_title(self, loc=’center’)


參數:此方法接受以下參數。

  • loc:此參數是可選參數,用於獲取哪個標題。

返回:此函數返回標題文本字符串。

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

範例1:

# Implementation of matplotlib function 
import os 
from matplotlib import font_manager as fm, rcParams 
import matplotlib.pyplot as plt 
   
fig, ax = plt.subplots() 
   
fpath = os.path.join(rcParams["datapath"],  
                     "fonts / ttf / cmr10.ttf") 
prop = fm.FontProperties(fname = fpath) 
fname = os.path.split(fpath)[1] 
ax.set_title('Title with special font:{}'.format(fname), 
             fontproperties = prop, fontsize = 14) 
  
w = ax.get_title() 
ax.text(0.2, 0.6, "Previously assigned title:\n\n"+str(w), 
        fontsize = 14) 
ax.set_title("matplotlib.axes.Axes.get_title() \ 
function Example\n", fontweight ="bold") 
plt.show()

輸出:

範例2:

# Implementation of matplotlib function 
import numpy as np 
import matplotlib.pyplot as plt 
   
x = np.arange(0.1, 5, 0.1) 
y = np.exp(-x) 
   
yerr = 0.1 + 0.1 * np.sqrt(x) 
   
fig, (ax, ax1) = plt.subplots(nrows = 1, 
                              ncols = 2, 
                              sharex = True) 
  
ax.errorbar(x, y, yerr = yerr, color ="green") 
ax.set_title('Title of Axes 1', fontweight ="bold") 
   
ax1.errorbar(x, y, yerr = yerr, errorevery = 5, 
             color ="green") 
ax1.set_title('Title of Axes 2', fontweight ="bold") 
  
w = ax.get_title() 
ww = ax1.get_title() 
ax.set_title("") 
ax1.set_title("") 
  
ax.set_xlabel(w) 
ax1.set_xlabel(ww) 
  
fig.suptitle("Previously assigned title of each Axes is\ 
 used at labels\n", fontweight ="bold") 
plt.show()

輸出:




相關用法


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