放置文本框
使用文本框装饰轴时,有两个有用的技巧是将文本放在轴坐标中(请参阅转换教程),因此文本不会随着x或y限制的变化而移动。 您还可以使用文本的bbox属性用Patch实例包围文本 - bbox关键字参数使用带有Patch属性的键的字典。

import numpy as npimport matplotlib.pyplot as pltnp.random.seed(19680801)fig, ax = plt.subplots()x = 30*np.random.randn(10000)mu = x.mean()median = np.median(x)sigma = x.std()textstr = '\n'.join((r'$\mu=%.2f$' % (mu, ),r'$\mathrm{median}=%.2f$' % (median, ),r'$\sigma=%.2f$' % (sigma, )))ax.hist(x, 50)# these are matplotlib.patch.Patch propertiesprops = dict(boxstyle='round', facecolor='wheat', alpha=0.5)# place a text box in upper left in axes coordsax.text(0.05, 0.95, textstr, transform=ax.transAxes, fontsize=14,verticalalignment='top', bbox=props)plt.show()
