Matplotlib是Python中的一個庫,它是數字的-NumPy庫的數學擴展。軸類包含大多數圖形元素:Axis,Tick,Line2D,Text,Polygon等,並設置坐標係。 Axes實例通過callbacks屬性支持回調。
matplotlib.axes.Axes.set_title()函數
matplotlib庫的軸模塊中的Axes.set_title()函數用於設置軸的標題。
用法: Axes.set_title(self, label, fontdict=None, loc=’center’, pad=None, **kwargs)
參數:此方法接受以下參數。
- label:此參數是用於標題的文本。
- fontdict:此參數是控製標題文本外觀的字典。
- loc:此參數用於設置標題{'center','left','right'}的位置。
- pad:此參數是標題距軸頂部的偏移量(以磅為單位)。
返回值:此方法返回代表標題的matplotlib文本實例。
以下示例說明了matplotlib.axes中的matplotlib.axes.Axes.set_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)
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, axs = plt.subplots(nrows = 1,
ncols = 2,
sharex = True)
ax = axs[0]
ax.errorbar(x, y, yerr = yerr,
color ="green")
ax.set_title('Title of Axes 1',
fontweight ="bold")
ax = axs[1]
ax.errorbar(x, y, yerr = yerr,
errorevery = 5,
color ="green")
ax.set_title('Title of Axes 2',
fontweight ="bold")
fig.suptitle('matplotlib.axes.Axes.set_title() \
function Example\n')
plt.show()
輸出:
相關用法
注:本文由純淨天空篩選整理自SHUBHAMSINGH10大神的英文原創作品 Matplotlib.axes.Axes.set_title() in Python。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。