Matplotlib是Python中的一個庫,它是數字的-NumPy庫的數學擴展。軸類包含大多數圖形元素:Axis,Tick,Line2D,Text,Polygon等,並設置坐標係。 Axes實例通過callbacks屬性支持回調。
matplotlib.axes.Axes.legend()函數
matplotlib庫的axiss模塊中的Axes.legend()函數用於在軸上放置圖例。
用法: Axes.legend(self, *args, **kwargs)
參數:此方法接受以下參數。
- labels:此參數是在藝術家旁邊顯示的標簽列表。
- handles:此參數是要添加到圖例的藝術家列表(線條,補丁)。
返回值:此方法返回matplotlib.legend.Legend實例。
以下示例說明了matplotlib.axes中的matplotlib.axes.Axes.legend()函數:
範例1:
# Implementation of matplotlib function
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
line1, = ax.plot([1, 2, 3],
label ="Line 1",
color ="black",
linewidth = 4,
linestyle =':')
line2, = ax.plot([3, 2, 1],
label ="Line 2",
color ="green",
linewidth = 4)
first_legend = ax.legend(handles =[line1],
loc ='upper center')
ax.add_artist(first_legend)
ax.legend(handles =[line2], loc ='lower center')
fig.suptitle('matplotlib.axes.Axes.legend() \
function Example\n', fontweight ="bold")
plt.show()
輸出:
範例2:
# Implementation of matplotlib function
import numpy as np
import matplotlib.pyplot as plt
np.random.seed(19680801)
fig, ax = plt.subplots()
for color in [ 'tab:green', 'tab:blue',
'tab:orange']:
n = 70
x, y = np.random.rand(2, n)
scale = 1000.0 * np.random.rand(n)
ax.scatter(x, y, c = color, s = scale,
label = color,
alpha = 0.35)
ax.legend()
ax.grid(True)
fig.suptitle('matplotlib.axes.Axes.legend() function\
Example\n', fontweight ="bold")
plt.show()
輸出:
相關用法
注:本文由純淨天空篩選整理自SHUBHAMSINGH10大神的英文原創作品 Matplotlib.axes.Axes.legend() in Python。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。