Matplotlib.artist.artist.set_label()
Python matplotlib库的artist模块中的set_label()方法 用于设置将在图例中显示的标签。
语法:
Artist.set_labelself, s)
参数:该方法只接受一个参数。
- s:通过调用str将该参数转换为字符串。
Returns:该方法不返回任何值。
下面的例子说明了matplotlib中的matplotlib.artist.artist.set_label()函数:
示例1
# Implementation of matplotlib function
from matplotlib.artist import Artist
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.collections import EllipseCollection
x = np.arange(10)
y = np.arange(15)
X, Y = np.meshgrid(x, y)
XY = np.column_stack((X.ravel(), Y.ravel()))
fig, ax = plt.subplots()
ec = EllipseCollection(10, 10, 5, units ='y',
offsets = XY * 0.5,
transOffset = ax.transData,
cmap ="bone")
ec.set_array((X * Y + X * X).ravel())
ax.add_collection(ec)
ax.autoscale_view()
ax.set_xlabel('X')
ax.set_ylabel('y')
cbar = plt.colorbar(ec)
cbar.set_label('X + Y')
fig.suptitle("""matplotlib.artist.Artist.set_label()
function Example""", fontweight="bold")
plt.show()
输出:
示例2
# Implementation of matplotlib function
from matplotlib.artist import Artist
import matplotlib.pyplot as plt
import numpy as np
np.random.seed(19680801)
n = 100000
x = np.random.standard_normal(n)
y = 2 * np.random.standard_normal(n)
z =[1, 2, 3, 4]
xmin = x.min()
xmax = x.max()
ymin = y.min()
ymax = y.max()
fig, ax = plt.subplots()
hb = ax.hexbin(x, y,
gridsize = 50,
bins ='log',
cmap ='PuBuGn')
ax.set(xlim =(xmin, xmax), ylim =(ymin, ymax))
cb = fig.colorbar(hb, ax = ax)
cb.set_label('log')
fig.suptitle("""matplotlib.artist.Artist.set_label()
function Example""", fontweight="bold")
plt.show()
输出: